Skip to content

Commit 04dca39

Browse files
committed
Test the corrupted .cmi bytes cases of read_cmi_string
A truncated value header, a truncated value and a malformed value each report a corrupted interface. Codecov had these branches uncovered. Signed-off-by: Jono Prest <jjprest@gmail.com>
1 parent 93e6375 commit 04dca39

1 file changed

Lines changed: 18 additions & 5 deletions

File tree

‎tests/ounit_tests/ounit_compiler_request_tests.ml‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,12 +1499,25 @@ let speculation_hook_tests _context =
14991499
first_build := false;
15001500
expect_code 0 (run_with_hooks root ~hooks "Dep.res");
15011501
assert_equal new_bytes (file_contents cmi_path);
1502+
let corrupted bytes =
1503+
try
1504+
ignore (Cmi_format.read_cmi_string "Dep.cmi" bytes);
1505+
false
1506+
with Cmi_format.Error (Corrupted_interface _) -> true
1507+
in
1508+
let magic_length = String.length Config.cmi_magic_number in
1509+
check (corrupted "Caml") "truncated bytes are a corrupted interface";
1510+
check
1511+
(corrupted (String.sub new_bytes 0 (magic_length + 1)))
1512+
"a truncated value header is a corrupted interface";
1513+
check
1514+
(corrupted
1515+
(String.sub new_bytes 0 (magic_length + Marshal.header_size + 1)))
1516+
"a truncated value is a corrupted interface";
15021517
check
1503-
(try
1504-
ignore (Cmi_format.read_cmi_string "Dep.cmi" "Caml");
1505-
false
1506-
with Cmi_format.Error _ -> true)
1507-
"truncated bytes are a corrupted interface";
1518+
(corrupted
1519+
(Config.cmi_magic_number ^ String.make Marshal.header_size 'x'))
1520+
"a malformed value is a corrupted interface";
15081521
(* The published file is older than the redirected bytes. *)
15091522
write root "Dep.res" "let value = 1\n";
15101523
expect_code 0 (run root "Dep.res" |> snd);

0 commit comments

Comments
 (0)