1
Fork 0

overflow tests

This commit is contained in:
Motiejus Jakštys 2022-02-23 10:12:23 +02:00 committed by Motiejus Jakštys
parent d555994960
commit 609ab3d2b6
1 changed files with 15 additions and 1 deletions

View File

@ -34,7 +34,7 @@ pub fn uvarint(buf: []const u8) error{Overflow}!Varint {
return Varint{ .value = x | (@as(u64, b) << s), .bytesRead = i + 1 }; return Varint{ .value = x | (@as(u64, b) << s), .bytesRead = i + 1 };
} }
x |= (@as(u64, b & 0x7f) << s); x |= (@as(u64, b & 0x7f) << s);
s += 7; s = try std.math.add(u6, s, 7);
} }
return Varint{ return Varint{
@ -88,3 +88,17 @@ test "uvarint" {
try testing.expectEqual(n, got.bytesRead); try testing.expectEqual(n, got.bytesRead);
} }
} }
const overflowTest = struct {
arr: []const u8,
};
test "overflow" {
for ([_][]const u8{
&[_]u8{ 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x2 },
&[_]u8{ 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x1, 0, 0 },
&[_]u8{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
}) |t| {
try testing.expectError(error.Overflow, uvarint(t));
}
}