From 76b10b5f5dd17b2c4e80f391883ec42500a85875 Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Tue, 8 Mar 2022 19:28:55 +0100 Subject: [varint] Add additional LEB128 tests. --- core/encoding/varint/leb128.odin | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'core/encoding') diff --git a/core/encoding/varint/leb128.odin b/core/encoding/varint/leb128.odin index 7640ba1fb..898c6af67 100644 --- a/core/encoding/varint/leb128.odin +++ b/core/encoding/varint/leb128.odin @@ -10,6 +10,8 @@ // the LEB128 format as used by DWARF debug info, Android .dex and other file formats. package varint +import "core:fmt" + // In theory we should use the bigint package. In practice, varints bigger than this indicate a corrupted file. // Instead we'll set limits on the values we'll encode/decode // 18 * 7 bits = 126, which means that a possible 19th byte may at most be `0b0000_0011`. @@ -29,6 +31,7 @@ decode_uleb128 :: proc(buf: []u8) -> (val: u128, size: int, err: Error) { for v, i in buf { size = i + 1 + // 18 * 7 bits = 126, which means that a possible 19th byte may at most be 0b0000_0011. if size == LEB128_MAX_BYTES && v > 0b0000_0011 { return 0, 0, .Value_Too_Large } @@ -60,8 +63,8 @@ decode_ileb128 :: proc(buf: []u8) -> (val: i128, size: int, err: Error) { for v in buf { size += 1 - // 18 * 7 bits = 126, which means that a possible 19th byte may at most be 0b0000_0011. - if size == LEB128_MAX_BYTES && v > 0b0000_0011 { + // 18 * 7 bits = 126, which including sign means we can have a 19th byte. + if size == LEB128_MAX_BYTES && v > 0x7f { return 0, 0, .Value_Too_Large } @@ -86,6 +89,7 @@ encode_uleb128 :: proc(buf: []u8, val: u128) -> (size: int, err: Error) { size += 1 if size > len(buf) { + fmt.println(val, buf[:size - 1]) return 0, .Buffer_Too_Small } -- cgit v1.2.3