From 609ab3d2b61d0a74c9fd52a4fd0356ab5cbfd794 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Wed, 23 Feb 2022 10:12:23 +0200 Subject: [PATCH] overflow tests --- src/varint.zig | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) 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)); + } +}