Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/deserialize.zig
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ pub fn deserialize(comptime T: type, allocator: Allocator, serialized: []const u
.int => {
const r = try sizeAndDataOffset(serialized);
if (r.offset + r.size > serialized.len) return error.RlpPayloadTooShort;
if (r.size > @sizeOf(T)) return error.RlpIntPayloadTooLong;
try safeReadSliceIntBig(T, serialized[r.offset .. r.offset + r.size], out);
return r.offset + r.size;
},
Expand Down Expand Up @@ -234,6 +235,15 @@ test "deserialize an integer" {
try expect(consumed == su16long.len);
}

test "deserialize an integer rejects oversized payload" {
// 0x83 = 0x80 + 3: a 3-byte byte-string payload, which is too large to
// fit into a u16 (2 bytes). The decoder must reject this rather than
// silently truncating the payload.
const rlp = [_]u8{ 0x83, 0x01, 0x02, 0x03 };
var out: u16 = undefined;
try expectError(error.RlpIntPayloadTooLong, deserialize(u16, std.testing.allocator, rlp[0..], &out));
}

test "deserialize a structure" {
const Person = struct {
age: u8,
Expand Down
Loading