diff --git a/src/varint.zig b/src/varint.zig index 7a4420c..8f0a925 100644 --- a/src/varint.zig +++ b/src/varint.zig @@ -34,7 +34,7 @@ pub fn uvarint(buf: []const u8) error{Overflow}!Varint { return Varint{ .value = x | (@as(u64, b) << s), .bytesRead = i + 1 }; } x |= (@as(u64, b & 0x7f) << s); - s += 7; + s = try std.math.add(u6, s, 7); } return Varint{ @@ -88,3 +88,17 @@ test "uvarint" { 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)); + } +}