add mem.readVarInt, fix InStream.readVarInt, fix stack traces

fixes a regression from b883bc8
This commit is contained in:
Andrew Kelley 2018-12-13 06:38:14 -05:00
parent 6395cf8d6b
commit e98ba5fc40
No known key found for this signature in database
GPG Key ID: 7C5F548F728501A9
3 changed files with 28 additions and 6 deletions

View File

@ -1200,7 +1200,7 @@ const Constant = struct {
fn asUnsignedLe(self: *const Constant) !u64 {
if (self.payload.len > @sizeOf(u64)) return error.InvalidDebugInfo;
if (self.signed) return error.InvalidDebugInfo;
return mem.readIntSliceLittle(u64, self.payload);
return mem.readVarInt(u64, self.payload, builtin.Endian.Little);
}
};

View File

@ -183,11 +183,12 @@ pub fn InStream(comptime ReadError: type) type {
return mem.readIntSlice(T, bytes, endian);
}
pub fn readVarInt(self: *Self, comptime T: type, endian: builtin.Endian, size: usize) !T {
assert(size <= @sizeOf(T));
var bytes: [@sizeOf(T)]u8 = undefined;
try self.readNoEof(bytes[0..size]);
return mem.readIntSlice(T, bytes, endian);
pub fn readVarInt(self: *Self, comptime ReturnType: type, endian: builtin.Endian, size: usize) !ReturnType {
assert(size <= @sizeOf(ReturnType));
var bytes_buf: [@sizeOf(ReturnType)]u8 = undefined;
const bytes = bytes_buf[0..size];
try self.readNoEof(bytes);
return mem.readVarInt(ReturnType, bytes, endian);
}
pub fn skipBytes(self: *Self, num_bytes: usize) !void {

View File

@ -407,6 +407,27 @@ test "mem.indexOf" {
assert(lastIndexOfScalar(u8, "boo", 'o').? == 2);
}
/// Reads an integer from memory with size equal to bytes.len.
/// T specifies the return type, which must be large enough to store
/// the result.
pub fn readVarInt(comptime ReturnType: type, bytes: []const u8, endian: builtin.Endian) ReturnType {
var result: ReturnType = 0;
switch (endian) {
builtin.Endian.Big => {
for (bytes) |b| {
result = (result << 8) | b;
}
},
builtin.Endian.Little => {
const ShiftType = math.Log2Int(ReturnType);
for (bytes) |b, index| {
result = result | (ReturnType(b) << @intCast(ShiftType, index * 8));
}
},
}
return result;
}
/// Reads an integer from memory with bit count specified by T.
/// The bit count of T must be evenly divisible by 8.
/// This function cannot fail and cannot cause undefined behavior.