Skip to content

Commit 1606dae

Browse files
LemonBoyandrewrk
authored andcommitted
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.
1 parent 1c02238 commit 1606dae

File tree

2 files changed

+21
-35
lines changed

2 files changed

+21
-35
lines changed

std/debug/leb128.zig

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -108,53 +108,37 @@ pub fn readILEB128Mem(comptime T: type, ptr: *[*]const u8) !T {
108108
}
109109
}
110110

111-
const OneByteReadInStream = struct {
112-
const Error = error{NoError};
113-
const Stream = std.io.InStream(Error);
114-
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;
111+
fn test_read_stream_ileb128(comptime T: type, encoded: []const u8) !T {
112+
var in_stream = std.io.SliceInStream.init(encoded);
113+
return try readILEB128(T, &in_stream.stream);
114+
}
131115

132-
dest[0] = self.str[self.curr];
133-
self.curr += 1;
134-
return 1;
135-
}
136-
};
116+
fn test_read_stream_uleb128(comptime T: type, encoded: []const u8) !T {
117+
var in_stream = std.io.SliceInStream.init(encoded);
118+
return try readULEB128(T, &in_stream.stream);
119+
}
137120

138121
fn test_read_ileb128(comptime T: type, encoded: []const u8) !T {
139-
var in_stream = OneByteReadInStream.init(encoded);
140-
const v1 = try readILEB128(T, &in_stream.stream);
122+
var in_stream = std.io.SliceInStream.init(encoded);
123+
const v1 = readILEB128(T, &in_stream.stream);
141124
var in_ptr = encoded.ptr;
142-
const v2 = try readILEB128Mem(T, &in_ptr);
125+
const v2 = readILEB128Mem(T, &in_ptr);
143126
testing.expectEqual(v1, v2);
144-
return v2;
127+
return v1;
145128
}
146129

147130
fn test_read_uleb128(comptime T: type, encoded: []const u8) !T {
148-
var in_stream = OneByteReadInStream.init(encoded);
149-
const v1 = try readULEB128(T, &in_stream.stream);
131+
var in_stream = std.io.SliceInStream.init(encoded);
132+
const v1 = readULEB128(T, &in_stream.stream);
150133
var in_ptr = encoded.ptr;
151-
const v2 = try readULEB128Mem(T, &in_ptr);
152-
return v2;
134+
const v2 = readULEB128Mem(T, &in_ptr);
135+
testing.expectEqual(v1, v2);
136+
return v1;
153137
}
154138

155139
test "deserialize signed LEB128" {
156140
// Truncated
157-
testing.expectError(error.EndOfStream, test_read_ileb128(i64, "\x80"));
141+
testing.expectError(error.EndOfStream, test_read_stream_ileb128(i64, "\x80"));
158142

159143
// Overflow
160144
testing.expectError(error.Overflow, test_read_ileb128(i8, "\x80\x80\x40"));
@@ -188,7 +172,7 @@ test "deserialize signed LEB128" {
188172

189173
test "deserialize unsigned LEB128" {
190174
// Truncated
191-
testing.expectError(error.EndOfStream, test_read_uleb128(u64, "\x80"));
175+
testing.expectError(error.EndOfStream, test_read_stream_uleb128(u64, "\x80"));
192176

193177
// Overflow
194178
testing.expectError(error.Overflow, test_read_uleb128(u8, "\x80\x80\x40"));

std/std.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,6 @@ test "std" {
9999
_ = @import("unicode.zig");
100100
_ = @import("valgrind.zig");
101101
_ = @import("zig.zig");
102+
103+
_ = @import("debug/leb128.zig");
102104
}

0 commit comments

Comments
 (0)