fix leb128 slice bug
Some checks are pending
format / format (push) Waiting to run
Build and Test / build (push) Waiting to run

This commit is contained in:
Simon Cruanes 2026-02-10 03:14:50 +00:00
parent 42c4f1c173
commit 2271ddedcc
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
2 changed files with 41 additions and 2 deletions

View file

@ -14,7 +14,7 @@ module Decode = struct
while !continue do while !continue do
if sl.len <= 0 then invalid_arg "out of bound"; if sl.len <= 0 then invalid_arg "out of bound";
incr n_consumed; incr n_consumed;
let b = Char.code (Bytes.get sl.bs !off) in let b = Char.code (Bytes.get sl.bs (sl.off + !off)) in
let cur = b land 0x7f in let cur = b land 0x7f in
if cur <> b then ( if cur <> b then (
(* at least one byte follows this one *) (* at least one byte follows this one *)
@ -39,7 +39,7 @@ module Decode = struct
while !continue do while !continue do
if sl.len <= 0 then invalid_arg "out of bound"; if sl.len <= 0 then invalid_arg "out of bound";
incr n_consumed; incr n_consumed;
let b = Char.code (Bytes.get sl.bs !off) in let b = Char.code (Bytes.get sl.bs (sl.off + !off)) in
let cur = b land 0x7f in let cur = b land 0x7f in
if cur <> b then ( if cur <> b then (
(* at least one byte follows this one *) (* at least one byte follows this one *)

View file

@ -226,5 +226,44 @@ assert_equal ~printer:Int64.to_string 500L v2;
assert_equal ~printer:string_of_int 2 n1; assert_equal ~printer:string_of_int 2 n1;
assert_equal ~printer:string_of_int 2 n2; assert_equal ~printer:string_of_int 2 n2;
true true
;;
t @@ fun () ->
(* Test decoding from a slice with non-zero offset *)
let bytes = Bytes.of_string "\x00\x00\x54\x00" in
let slice = Slice.create ~off:2 ~len:1 bytes in
assert_equal
~printer:(fun c -> Printf.sprintf "0x%02x" (Char.code c))
'\x54' (Slice.get slice 0);
let v, n = Leb128.Decode.int_truncate slice 0 in
assert_equal ~printer:string_of_int 42 v;
assert_equal ~printer:string_of_int 1 n;
true
;;
t @@ fun () ->
(* Test decoding u64 from a slice with non-zero offset *)
let bytes = Bytes.of_string "\xFF\xFF\x2A\x00" in
let slice = Slice.create ~off:2 ~len:1 bytes in
assert_equal
~printer:(fun c -> Printf.sprintf "0x%02x" (Char.code c))
'\x2A' (Slice.get slice 0);
let v, n = Leb128.Decode.u64 slice 0 in
assert_equal ~printer:Int64.to_string 42L v;
assert_equal ~printer:string_of_int 1 n;
true
;;
t @@ fun () ->
(* Test decoding from a sub-slice *)
let buf = Buf.create () in
Buf.append_string buf "padding";
Leb128.Encode.int buf 42;
let slice = Buf.to_slice buf in
let sub_slice = Slice.sub slice 7 (Slice.len slice - 7) in
let v, n = Leb128.Decode.int_truncate sub_slice 0 in
assert_equal ~printer:string_of_int 42 v;
assert_equal ~printer:string_of_int 1 n;
true
let () = Containers_testlib.run_all ~descr:"test leb128" [ get () ] let () = Containers_testlib.run_all ~descr:"test leb128" [ get () ]