aboutsummaryrefslogtreecommitdiff
path: root/std/debug/leb128.zig
diff options
context:
space:
mode:
authorLemonBoy <thatlemon@gmail.com>2019-05-15 09:41:38 +0200
committerAndrew Kelley <andrew@ziglang.org>2019-05-15 11:11:16 -0400
commit787fd0f1bea262d134f06e5913554b8ca086655f (patch)
tree979f546155d5ba67ae85d3c97c508d39e2f62ced /std/debug/leb128.zig
parent057a5d4898f70c6a8169c99375fbb8631e539051 (diff)
downloadzig-787fd0f1bea262d134f06e5913554b8ca086655f.tar.gz
zig-787fd0f1bea262d134f06e5913554b8ca086655f.zip
Fix off-by-one error in LEB128 parsing
Diffstat (limited to 'std/debug/leb128.zig')
-rw-r--r--std/debug/leb128.zig32
1 files changed, 30 insertions, 2 deletions
diff --git a/std/debug/leb128.zig b/std/debug/leb128.zig
index 2801877839..cb59c5b0d2 100644
--- a/std/debug/leb128.zig
+++ b/std/debug/leb128.zig
@@ -46,7 +46,7 @@ pub fn readULEB128Mem(comptime T: type, ptr: *[*]const u8) !T {
result |= operand;
if ((byte & 0x80) == 0) {
- ptr.* += i;
+ ptr.* += i + 1;
return result;
}
@@ -114,7 +114,7 @@ pub fn readILEB128Mem(comptime T: type, ptr: *[*]const u8) !T {
if (shift < T.bit_count and (byte & 0x40) != 0) {
result |= @bitCast(UT, @intCast(T, -1)) << @intCast(ShiftT, shift);
}
- ptr.* += i;
+ ptr.* += i + 1;
return @bitCast(T, result);
}
}
@@ -148,6 +148,28 @@ fn test_read_uleb128(comptime T: type, encoded: []const u8) !T {
return v1;
}
+fn test_read_ileb128_seq(comptime T: type, comptime N: usize, encoded: []const u8) void {
+ var in_stream = std.io.SliceInStream.init(encoded);
+ var in_ptr = encoded.ptr;
+ var i: usize = 0;
+ while (i < N) : (i += 1) {
+ const v1 = readILEB128(T, &in_stream.stream);
+ const v2 = readILEB128Mem(T, &in_ptr);
+ testing.expectEqual(v1, v2);
+ }
+}
+
+fn test_read_uleb128_seq(comptime T: type, comptime N: usize, encoded: []const u8) void {
+ var in_stream = std.io.SliceInStream.init(encoded);
+ var in_ptr = encoded.ptr;
+ var i: usize = 0;
+ while (i < N) : (i += 1) {
+ const v1 = readULEB128(T, &in_stream.stream);
+ const v2 = readULEB128Mem(T, &in_ptr);
+ testing.expectEqual(v1, v2);
+ }
+}
+
test "deserialize signed LEB128" {
// Truncated
testing.expectError(error.EndOfStream, test_read_stream_ileb128(i64, "\x80"));
@@ -188,6 +210,9 @@ test "deserialize signed LEB128" {
testing.expect((try test_read_ileb128(i64, "\xff\x80\x00")) == 0x7f);
testing.expect((try test_read_ileb128(i64, "\x80\x81\x00")) == 0x80);
testing.expect((try test_read_ileb128(i64, "\x80\x81\x80\x00")) == 0x80);
+
+ // Decode sequence of SLEB128 values
+ test_read_ileb128_seq(i64, 4, "\x81\x01\x3f\x80\x7f\x80\x80\x80\x00");
}
test "deserialize unsigned LEB128" {
@@ -225,4 +250,7 @@ test "deserialize unsigned LEB128" {
testing.expect((try test_read_uleb128(u64, "\xff\x80\x00")) == 0x7f);
testing.expect((try test_read_uleb128(u64, "\x80\x81\x00")) == 0x80);
testing.expect((try test_read_uleb128(u64, "\x80\x81\x80\x00")) == 0x80);
+
+ // Decode sequence of ULEB128 values
+ test_read_uleb128_seq(u64, 4, "\x81\x01\x3f\x80\x7f\x80\x80\x80\x00");
}