mirror of
https://git.xeondev.com/LR/S.git
synced 2026-03-21 23:22:21 +01:00
fix(proto): all varints should be backed by u64s over the wire
This commit is contained in:
@@ -256,14 +256,19 @@ fn decodeField(r: *Io.Reader, allocator: Allocator, comptime T: type, wire_type:
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn readVarInt(r: *Io.Reader, comptime T: type) !T {
|
fn readVarInt(r: *Io.Reader, comptime T: type) !T {
|
||||||
var shift: std.math.Log2Int(T) = 0;
|
const int = @typeInfo(T).int;
|
||||||
var result: T = 0;
|
var shift: std.math.Log2Int(u64) = 0;
|
||||||
|
var result: u64 = 0;
|
||||||
|
|
||||||
while (true) : (shift += 7) {
|
while (true) : (shift += 7) {
|
||||||
const byte = try r.takeByte();
|
const byte = try r.takeByte();
|
||||||
result |= @as(T, @intCast(byte & 0x7F)) << shift;
|
result |= @as(u64, byte & 0x7F) << shift;
|
||||||
if ((byte & 0x80) != 0x80) return result;
|
if ((byte & 0x80) != 0x80) return switch (int.signedness) {
|
||||||
if (shift >= @bitSizeOf(T) - 7) return error.MalformedProtobuf;
|
.unsigned => @truncate(result),
|
||||||
|
.signed => @bitCast(@as(@Int(.unsigned, int.bits), @truncate(result))),
|
||||||
|
};
|
||||||
|
|
||||||
|
if (shift >= @bitSizeOf(u64) - 7) return error.MalformedProtobuf;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user