diff options
| author | Jeroen van Rijn <Kelimion@users.noreply.github.com> | 2022-03-08 19:28:55 +0100 |
|---|---|---|
| committer | Jeroen van Rijn <Kelimion@users.noreply.github.com> | 2022-03-08 19:28:55 +0100 |
| commit | 76b10b5f5dd17b2c4e80f391883ec42500a85875 (patch) | |
| tree | b0dae3ad608ed2c122a13f66fffa85a39398cc47 /core/encoding/varint | |
| parent | e76a5d8e12ae3d6e8e75e8df53a30bfbcc66b829 (diff) | |
[varint] Add additional LEB128 tests.
Diffstat (limited to 'core/encoding/varint')
| -rw-r--r-- | core/encoding/varint/leb128.odin | 8 |
1 files changed, 6 insertions, 2 deletions
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 } |