From 9256fe5e54acdc3abd8d13c21d31852e9842767b Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Wed, 8 Jun 2022 16:40:33 +1000 Subject: [PATCH] Have std/bzip2 check the number of sections Updates #79 --- release/c/wuffs-unsupported-snapshot.c | 10 ++++- std/bzip2/decode_bzip2.wuffs | 1 - std/bzip2/decode_huffman_fast.wuffs | 3 ++ std/bzip2/decode_huffman_slow.wuffs | 3 ++ test/c/std/bzip2.c | 19 ++++++++ .../abraca.txt.bz2.make-artificial.txt | 14 +++--- .../bad-number-of-sections.bz2 | Bin 0 -> 54 bytes ...number-of-sections.bz2.make-artificial.txt | 41 ++++++++++++++++++ 8 files changed, 81 insertions(+), 10 deletions(-) create mode 100644 test/data/artificial-bzip2/bad-number-of-sections.bz2 create mode 100644 test/data/artificial-bzip2/bad-number-of-sections.bz2.make-artificial.txt diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index ef19531c..689f3145 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -6827,7 +6827,6 @@ extern const char wuffs_bzip2__error__bad_block_header[]; extern const char wuffs_bzip2__error__bad_block_length[]; extern const char wuffs_bzip2__error__bad_checksum[]; extern const char wuffs_bzip2__error__bad_header[]; -extern const char wuffs_bzip2__error__bad_number_of_huffman_codes[]; extern const char wuffs_bzip2__error__bad_number_of_sections[]; extern const char wuffs_bzip2__error__unsupported_huffman_code[]; extern const char wuffs_bzip2__error__unsupported_block_randomization[]; @@ -24866,7 +24865,6 @@ const char wuffs_bzip2__error__bad_block_header[] = "#bzip2: bad block header"; const char wuffs_bzip2__error__bad_block_length[] = "#bzip2: bad block length"; const char wuffs_bzip2__error__bad_checksum[] = "#bzip2: bad checksum"; const char wuffs_bzip2__error__bad_header[] = "#bzip2: bad header"; -const char wuffs_bzip2__error__bad_number_of_huffman_codes[] = "#bzip2: bad number of Huffman codes"; const char wuffs_bzip2__error__bad_number_of_sections[] = "#bzip2: bad number of sections"; const char wuffs_bzip2__error__unsupported_huffman_code[] = "#bzip2: unsupported Huffman code"; const char wuffs_bzip2__error__unsupported_block_randomization[] = "#bzip2: unsupported block randomization"; @@ -26192,6 +26190,10 @@ wuffs_bzip2__decoder__decode_huffman_fast( } else { v_ticks = 49; v_section += 1; + if (v_section >= self->private_impl.f_num_sections) { + status = wuffs_base__make_status(wuffs_bzip2__error__bad_number_of_sections); + goto exit; + } v_which = WUFFS_BZIP2__CLAMP_TO_5[(self->private_data.f_huffman_selectors[(v_section & 32767)] & 7)]; } v_bits |= (wuffs_base__peek_u32be__no_bounds_check(iop_a_src) >> v_n_bits); @@ -26311,6 +26313,10 @@ wuffs_bzip2__decoder__decode_huffman_slow( } else { self->private_impl.f_decode_huffman_ticks = 49; self->private_impl.f_decode_huffman_section += 1; + if (self->private_impl.f_decode_huffman_section >= self->private_impl.f_num_sections) { + status = wuffs_base__make_status(wuffs_bzip2__error__bad_number_of_sections); + goto exit; + } self->private_impl.f_decode_huffman_which = WUFFS_BZIP2__CLAMP_TO_5[(self->private_data.f_huffman_selectors[(self->private_impl.f_decode_huffman_section & 32767)] & 7)]; } v_node_index = 0; diff --git a/std/bzip2/decode_bzip2.wuffs b/std/bzip2/decode_bzip2.wuffs index fbe81b80..e13021b9 100644 --- a/std/bzip2/decode_bzip2.wuffs +++ b/std/bzip2/decode_bzip2.wuffs @@ -18,7 +18,6 @@ pub status "#bad block header" pub status "#bad block length" pub status "#bad checksum" pub status "#bad header" -pub status "#bad number of Huffman codes" pub status "#bad number of sections" pub status "#unsupported Huffman code" pub status "#unsupported block randomization" diff --git a/std/bzip2/decode_huffman_fast.wuffs b/std/bzip2/decode_huffman_fast.wuffs index 7c865381..c4e5df02 100644 --- a/std/bzip2/decode_huffman_fast.wuffs +++ b/std/bzip2/decode_huffman_fast.wuffs @@ -45,6 +45,9 @@ pri func decoder.decode_huffman_fast!(src: base.io_reader) base.status { } else { ticks = 49 section ~mod+= 1 + if section >= this.num_sections { + return "#bad number of sections" + } which = CLAMP_TO_5[this.huffman_selectors[section & 32767] & 7] } diff --git a/std/bzip2/decode_huffman_slow.wuffs b/std/bzip2/decode_huffman_slow.wuffs index 91777d3e..e22cb035 100644 --- a/std/bzip2/decode_huffman_slow.wuffs +++ b/std/bzip2/decode_huffman_slow.wuffs @@ -31,6 +31,9 @@ pri func decoder.decode_huffman_slow?(src: base.io_reader) { } else { this.decode_huffman_ticks = 49 this.decode_huffman_section ~mod+= 1 + if this.decode_huffman_section >= this.num_sections { + return "#bad number of sections" + } this.decode_huffman_which = CLAMP_TO_5[this.huffman_selectors[this.decode_huffman_section & 32767] & 7] } diff --git a/test/c/std/bzip2.c b/test/c/std/bzip2.c index a772c44c..5e5ccaa8 100644 --- a/test/c/std/bzip2.c +++ b/test/c/std/bzip2.c @@ -74,6 +74,11 @@ golden_test g_bzip2_256_bytes_gt = { .src_filename = "test/data/256.bytes.bz2", }; +golden_test g_bzip2_bad_number_of_sections_gt = { + .want_filename = "test/data/0.bytes", + .src_filename = "test/data/artificial-bzip2/bad-number-of-sections.bz2", +}; + golden_test g_bzip2_midsummer_gt = { .want_filename = "test/data/midsummer.txt", .src_filename = "test/data/midsummer.txt.bz2", @@ -137,6 +142,19 @@ test_wuffs_bzip2_decode_256_bytes() { UINT64_MAX, UINT64_MAX); } +const char* // +test_wuffs_bzip2_decode_bad_number_of_sections() { + CHECK_FOCUS(__func__); + const char* have = + do_test_io_buffers(wuffs_bzip2_decode, &g_bzip2_bad_number_of_sections_gt, + UINT64_MAX, UINT64_MAX); + const char* want = wuffs_bzip2__error__bad_number_of_sections; + if (have != want) { + RETURN_FAIL("have \"%s\", want \"%s\"", have, want); + } + return NULL; +} + const char* // test_wuffs_bzip2_decode_midsummer() { CHECK_FOCUS(__func__); @@ -227,6 +245,7 @@ bench_mimic_bzip2_decode_100k() { proc g_tests[] = { test_wuffs_bzip2_decode_256_bytes, + test_wuffs_bzip2_decode_bad_number_of_sections, test_wuffs_bzip2_decode_interface, test_wuffs_bzip2_decode_midsummer, test_wuffs_bzip2_decode_pi, diff --git a/test/data/artificial-bzip2/abraca.txt.bz2.make-artificial.txt b/test/data/artificial-bzip2/abraca.txt.bz2.make-artificial.txt index abfac121..40971524 100644 --- a/test/data/artificial-bzip2/abraca.txt.bz2.make-artificial.txt +++ b/test/data/artificial-bzip2/abraca.txt.bz2.make-artificial.txt @@ -1,7 +1,7 @@ # Feed this file to script/make-artificial.go # This script generates test/data/abraca.txt.bz2 exactly, following the -# description in std/bzip2/README.md and its sections (a) ..= (s). +# description in std/bzip2/README.md and its bitslices (a) ..= (s). # # By itself, this isn't a very interesting foo.make-artificial.txt file, in # that it doesn't make *artificial* bzip2 test data (as opposed to test data @@ -10,7 +10,7 @@ make bzip2 -# Sections (a) ..= (g) +# Bitslices (a) ..= (g) bits 16 0x425A bits 8 0x68 bits 8 0x39 @@ -19,21 +19,21 @@ bits 32 0x76A70995 bits 1 0x0 bits 24 0x000001 -# Sections (h) ..= (j) +# Bitslices (h) ..= (j) bits 16 0x0300 bits 16 0x7000 bits 16 0x2000 -# Sections (k) ..= (m) +# Bitslices (k) ..= (m) bits 3 0x2 bits 15 0x0001 bits 1 0x0 -# Sections (n) ..= (o) +# Bitslices (n) ..= (o) bits 19 0x0CD34 bits 19 0x0CD34 -# Section (p) +# Bitslice (p) bits 3 0x6 bits 2 0x0 bits 2 0x1 @@ -42,7 +42,7 @@ bits 3 0x4 bits 2 0x1 bits 3 0x7 -# Sections (q) ..= (s) +# Bitslices (q) ..= (s) bits 48 0x177245385090 bits 32 0x76A70995 bits 5 0x00 diff --git a/test/data/artificial-bzip2/bad-number-of-sections.bz2 b/test/data/artificial-bzip2/bad-number-of-sections.bz2 new file mode 100644 index 0000000000000000000000000000000000000000..a79925d2b4dc3ac68f1c60fd6264f2b0806b8a72 GIT binary patch literal 54 zcmZ>Y%CIzaj8qGbEL+Ywm4ShwvB82tfI)#laaM-0F9Q-_?%t_1N5jK9xaCv>08#f0 Aa{vGU literal 0 HcmV?d00001 diff --git a/test/data/artificial-bzip2/bad-number-of-sections.bz2.make-artificial.txt b/test/data/artificial-bzip2/bad-number-of-sections.bz2.make-artificial.txt new file mode 100644 index 00000000..77229196 --- /dev/null +++ b/test/data/artificial-bzip2/bad-number-of-sections.bz2.make-artificial.txt @@ -0,0 +1,41 @@ +# Feed this file to script/make-artificial.go + +# This modifies abraca.txt.bz2.make-artificial.txt to have 51 Huffman-coded +# symbols (bitslice (p)) but only 1 section (bitslice (l)). At 50 symbols per +# section, this should be invalid. + +make bzip2 + +# Bitslices (a) ..= (g) +bits 16 0x425A +bits 8 0x68 +bits 8 0x39 +bits 48 0x314159265359 +bits 32 0x76A70995 +bits 1 0x0 +bits 24 0x000001 + +# Bitslices (h) ..= (j) +bits 16 0x0300 +bits 16 0x7000 +bits 16 0x2000 + +# Bitslices (k) ..= (m) +bits 3 0x2 +bits 15 0x0001 +bits 1 0x0 + +# Bitslices (n) ..= (o) +bits 19 0x0CD34 +bits 19 0x0CD34 + +# Bitslice (p) +repeat 50 [ + bits 2 0x0 +] +bits 3 0x7 + +# Bitslices (q) ..= (s) +bits 48 0x177245385090 +bits 32 0x76A70995 +bits 5 0x00