compression/tests: calm the static analysts (CID: numerous)
authorDouglas Bagnall <douglas.bagnall@catalyst.net.nz>
Mon, 5 Dec 2022 22:26:47 +0000 (11:26 +1300)
committerJeremy Allison <jra@samba.org>
Mon, 19 Dec 2022 22:32:35 +0000 (22:32 +0000)
None of our test vectors are 18446744073709551615 bytes long, which
means we can know an `expected_length == returned_length` check will
catch the case where the compression function returns -1 for error. We
know that, but Coverity doesn't.

It's the same thing over and over again, in two different patterns:

>>>     CID 1517301:  Memory - corruptions  (OVERRUN)
>>>     Calling "memcmp" with "original.data" and "original.length" is
suspicious because of the very large index, 18446744073709551615. The index
may be due to a negative parameter being interpreted as unsigned.
393      if (original.length != decomp_written ||
394          memcmp(decompressed.data,
395         original.data,
396         original.length) != 0) {
397      debug_message("\033[1;31mgot %zd, expected %zu\033[0m\n",
398            decomp_written,

*** CID 1517299:  Memory - corruptions  (OVERRUN)
/lib/compression/tests/test_lzxpress_plain.c: 296 in
test_lzxpress_plain_decompress_more_compressed_files()
290      debug_start_timer();
291      written = lzxpress_decompress(p.compressed.data,
292            p.compressed.length,
293            dest,
294            p.decompressed.length);
295      debug_end_timer("decompress", p.decompressed.length);
>>>     CID 1517299:  Memory - corruptions  (OVERRUN)
>>>     Calling "memcmp" with "p.decompressed.data" and
"p.decompressed.length" is suspicious because of the very large index,
18446744073709551615. The index may be due to a negative parameter being
interpreted as unsigned.
296      if (written == p.decompressed.length &&
297          memcmp(dest, p.decompressed.data, p.decompressed.length)
== 0) {
298      debug_message("\033[1;32mdecompressed %s!

Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Jeremy Allison <jra@samba.org>
lib/compression/tests/test_lzx_huffman.c
lib/compression/tests/test_lzxpress_plain.c

index 3a055183f7b5b835ab70d65270052df263861537..647a01fa3c08f8bd1cd8e737ba1345b05cb08778 100644 (file)
@@ -344,6 +344,7 @@ static void test_lzxpress_huffman_decompress(void **state)
                                                      p.compressed.length,
                                                      dest,
                                                      p.decompressed.length);
+               assert_int_not_equal(written, -1);
                assert_int_equal(written, p.decompressed.length);
 
                assert_memory_equal(dest, p.decompressed.data, p.decompressed.length);
@@ -368,6 +369,7 @@ static void test_lzxpress_huffman_compress(void **state)
                                                           p.decompressed.length,
                                                           &dest);
 
+               assert_int_not_equal(written, -1);
                assert_int_equal(written, p.compressed.length);
                assert_memory_equal(dest, p.compressed.data, p.compressed.length);
                talloc_free(dest);
@@ -444,7 +446,8 @@ static void test_lzxpress_huffman_decompress_files(void **state)
                                                      dest,
                                                      p.decompressed.length);
                debug_end_timer("decompress", p.decompressed.length);
-               if (written == p.decompressed.length &&
+               if (written != -1 &&
+                   written == p.decompressed.length &&
                    memcmp(dest, p.decompressed.data, p.decompressed.length) == 0) {
                        debug_message("\033[1;32mdecompressed %s!\033[0m\n", p.name);
                        score++;
index 17e5a26207b01dc9fbce3aaa47b3bec2ed38d056..1264f48530b0c4f5ccbc948a48fa8534aa4aaeb8 100644 (file)
@@ -294,7 +294,8 @@ static void test_lzxpress_plain_decompress_more_compressed_files(void **state)
                                              dest,
                                              p.decompressed.length);
                debug_end_timer("decompress", p.decompressed.length);
-               if (written == p.decompressed.length &&
+               if (written != -1 &&
+                   written == p.decompressed.length &&
                    memcmp(dest, p.decompressed.data, p.decompressed.length) == 0) {
                        debug_message("\033[1;32mdecompressed %s!\033[0m\n", p.name);
                        score++;
@@ -790,7 +791,7 @@ static void test_msft_data1(void **state)
                                   strlen(fixed_data),
                                   out,
                                   talloc_get_size(out));
-
+       assert_int_not_equal(c_size, -1);
        assert_int_equal(c_size, sizeof(fixed_out));
        assert_memory_equal(out, fixed_out, c_size);
        out2  = talloc_size(tmp_ctx, strlen(fixed_data));
@@ -798,7 +799,7 @@ static void test_msft_data1(void **state)
                                     sizeof(fixed_out),
                                     out2,
                                     talloc_get_size(out2));
-
+       assert_int_not_equal(c_size, -1);
        assert_int_equal(c_size, strlen(fixed_data));
        assert_memory_equal(out2, fixed_data, c_size);
 
@@ -830,7 +831,7 @@ static void test_msft_data2(void **state)
                                   strlen(fixed_data),
                                   out,
                                   talloc_get_size(out));
-
+       assert_int_not_equal(c_size, -1);
        assert_int_equal(c_size, sizeof(fixed_out));
        assert_memory_equal(out, fixed_out, c_size);
 
@@ -840,6 +841,7 @@ static void test_msft_data2(void **state)
                                     out2,
                                     talloc_get_size(out2));
 
+       assert_int_not_equal(c_size, -1);
        assert_int_equal(c_size, strlen(fixed_data));
        assert_memory_equal(out2, fixed_data, c_size);
 
@@ -877,6 +879,7 @@ static void test_lzxpress(void **state)
                                   out,
                                   talloc_get_size(out));
 
+       assert_int_not_equal(c_size, -1);
        assert_int_equal(c_size, sizeof(fixed_out));
        assert_memory_equal(out, fixed_out, c_size);
 
@@ -886,6 +889,7 @@ static void test_lzxpress(void **state)
                                     out2,
                                     talloc_get_size(out2));
 
+       assert_int_not_equal(c_size, -1);
        assert_int_equal(c_size, strlen(fixed_data));
        assert_memory_equal(out2, fixed_data, c_size);
 
@@ -895,6 +899,7 @@ static void test_lzxpress(void **state)
                                     out3,
                                     talloc_get_size(out3));
 
+       assert_int_not_equal(c_size, -1);
        assert_int_equal(c_size, strlen(fixed_data));
        assert_memory_equal(out3, fixed_data, c_size);
 
@@ -927,6 +932,7 @@ static void test_lzxpress2(void **state)
                                   out,
                                   talloc_get_size(out));
 
+       assert_int_not_equal(c_size, -1);
        assert_int_equal(c_size, sizeof(fixed_out));
        assert_memory_equal(out, fixed_out, c_size);
 
@@ -936,6 +942,7 @@ static void test_lzxpress2(void **state)
                                     out2,
                                     talloc_get_size(out2));
 
+       assert_int_not_equal(c_size, -1);
        assert_int_equal(c_size, strlen(fixed_data));
        assert_memory_equal(out2, fixed_data, c_size);
 
@@ -971,6 +978,7 @@ static void test_lzxpress3(void **state)
                                   out,
                                   talloc_get_size(out));
 
+       assert_int_not_equal(c_size, -1);
        assert_int_equal(c_size, sizeof(fixed_out));
        assert_memory_equal(out, fixed_out, c_size);
 
@@ -980,6 +988,7 @@ static void test_lzxpress3(void **state)
                                     out2,
                                     talloc_get_size(out2));
 
+       assert_int_not_equal(c_size, -1);
        assert_int_equal(c_size, strlen(fixed_data));
        assert_memory_equal(out2, fixed_data, c_size);
 
@@ -1015,6 +1024,7 @@ static void test_lzxpress4(void **state)
                                   out,
                                   talloc_get_size(out));
 
+       assert_int_not_equal(c_size, -1);
        assert_int_equal(c_size, sizeof(fixed_out));
        assert_memory_equal(out, fixed_out, c_size);
 
@@ -1024,6 +1034,7 @@ static void test_lzxpress4(void **state)
                                     out2,
                                     talloc_get_size(out2));
 
+       assert_int_not_equal(c_size, -1);
        assert_int_equal(c_size, strlen(fixed_data));
        assert_memory_equal(out2, fixed_data, c_size);
 
@@ -1135,6 +1146,7 @@ static void test_lzxpress_round_trip(void **state)
                                        data,
                                        alloc_size);
 
+               assert_int_not_equal(len, -1);
                assert_int_equal(len, comp.length);
 
                assert_memory_equal(comp.data, data, len);
@@ -1144,6 +1156,7 @@ static void test_lzxpress_round_trip(void **state)
                                          data,
                                          alloc_size);
 
+               assert_int_not_equal(len, -1);
                assert_int_equal(len, uncomp.length);
 
                assert_memory_equal(uncomp.data, data, len);