authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2019-05-10 10:26:43+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-10 08:40:36-04:00
log1606dae7286daac67b5eb2d91108f558ab7b2b00
treee132ffb990a6b888d59ec90923f96b3d27ae50ec
parent1c0223899cfe6c1c73f186330f6eff2cc9e78579

Fix erroneous test case

The *Mem variants cannot return EndOfStream and are generally unsafe to use. Proper order of checks, try both the variants and make sure they return the same error/result. Run the leb128.zig tests.

2 files changed, 21 insertions(+), 35 deletions(-)

std/debug/leb128.zig+19-35
...@@ -108,53 +108,37 @@ pub fn readILEB128Mem(comptime T: type, ptr: *[*]const u8) !T {...@@ -108,53 +108,37 @@ pub fn readILEB128Mem(comptime T: type, ptr: *[*]const u8) !T {
108 }108 }
109}109}
110110
111const OneByteReadInStream = struct {111fn test_read_stream_ileb128(comptime T: type, encoded: []const u8) !T {
112 const Error = error{NoError};112 var in_stream = std.io.SliceInStream.init(encoded);
113 const Stream = std.io.InStream(Error);113 return try readILEB128(T, &in_stream.stream);
114114}
115 stream: Stream,
116 str: []const u8,
117 curr: usize,
118
119 fn init(str: []const u8) @This() {
120 return @This(){
121 .stream = Stream{ .readFn = readFn },
122 .str = str,
123 .curr = 0,
124 };
125 }
126
127 fn readFn(in_stream: *Stream, dest: []u8) Error!usize {
128 const self = @fieldParentPtr(@This(), "stream", in_stream);
129 if (self.str.len <= self.curr or dest.len == 0)
130 return 0;
131115
132 dest[0] = self.str[self.curr];116fn test_read_stream_uleb128(comptime T: type, encoded: []const u8) !T {
133 self.curr += 1;117 var in_stream = std.io.SliceInStream.init(encoded);
134 return 1;118 return try readULEB128(T, &in_stream.stream);
135 }119}
136};
137120
138fn test_read_ileb128(comptime T: type, encoded: []const u8) !T {121fn test_read_ileb128(comptime T: type, encoded: []const u8) !T {
139 var in_stream = OneByteReadInStream.init(encoded);122 var in_stream = std.io.SliceInStream.init(encoded);
140 const v1 = try readILEB128(T, &in_stream.stream);123 const v1 = readILEB128(T, &in_stream.stream);
141 var in_ptr = encoded.ptr;124 var in_ptr = encoded.ptr;
142 const v2 = try readILEB128Mem(T, &in_ptr);125 const v2 = readILEB128Mem(T, &in_ptr);
143 testing.expectEqual(v1, v2);126 testing.expectEqual(v1, v2);
144 return v2;127 return v1;
145}128}
146129
147fn test_read_uleb128(comptime T: type, encoded: []const u8) !T {130fn test_read_uleb128(comptime T: type, encoded: []const u8) !T {
148 var in_stream = OneByteReadInStream.init(encoded);131 var in_stream = std.io.SliceInStream.init(encoded);
149 const v1 = try readULEB128(T, &in_stream.stream);132 const v1 = readULEB128(T, &in_stream.stream);
150 var in_ptr = encoded.ptr;133 var in_ptr = encoded.ptr;
151 const v2 = try readULEB128Mem(T, &in_ptr);134 const v2 = readULEB128Mem(T, &in_ptr);
152 return v2;135 testing.expectEqual(v1, v2);
136 return v1;
153}137}
154138
155test "deserialize signed LEB128" {139test "deserialize signed LEB128" {
156 // Truncated140 // Truncated
157 testing.expectError(error.EndOfStream, test_read_ileb128(i64, "\x80"));141 testing.expectError(error.EndOfStream, test_read_stream_ileb128(i64, "\x80"));
158142
159 // Overflow143 // Overflow
160 testing.expectError(error.Overflow, test_read_ileb128(i8, "\x80\x80\x40"));144 testing.expectError(error.Overflow, test_read_ileb128(i8, "\x80\x80\x40"));
...@@ -188,7 +172,7 @@ test "deserialize signed LEB128" {...@@ -188,7 +172,7 @@ test "deserialize signed LEB128" {
188172
189test "deserialize unsigned LEB128" {173test "deserialize unsigned LEB128" {
190 // Truncated174 // Truncated
191 testing.expectError(error.EndOfStream, test_read_uleb128(u64, "\x80"));175 testing.expectError(error.EndOfStream, test_read_stream_uleb128(u64, "\x80"));
192176
193 // Overflow177 // Overflow
194 testing.expectError(error.Overflow, test_read_uleb128(u8, "\x80\x80\x40"));178 testing.expectError(error.Overflow, test_read_uleb128(u8, "\x80\x80\x40"));
std/std.zig+2
...@@ -99,4 +99,6 @@ test "std" {...@@ -99,4 +99,6 @@ test "std" {
99 _ = @import("unicode.zig");99 _ = @import("unicode.zig");
100 _ = @import("valgrind.zig");100 _ = @import("valgrind.zig");
101 _ = @import("zig.zig");101 _ = @import("zig.zig");
102
103 _ = @import("debug/leb128.zig");
102}104}