diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index c49f2276..a87fa357 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -41028,7 +41028,7 @@ wuffs_jpeg__decoder__fill_bitstream( label__0__break:; if (((uint64_t)(io2_a_src - iop_a_src)) > 1u) { if ((wuffs_base__peek_u8be__no_bounds_check(iop_a_src) >= 255u) && ((wuffs_base__peek_u16le__no_bounds_check(iop_a_src) >> 8u) > 0u)) { - v_new_wi = (wuffs_base__u32__min(v_wi, 2016u) + 32u); + v_new_wi = (wuffs_base__u32__min(v_wi, 1784u) + 264u); if (v_wi < v_new_wi) { wuffs_base__bulk_memset(&self->private_data.f_bitstream_buffer[v_wi], (v_new_wi - v_wi), 0u); } @@ -41676,11 +41676,15 @@ wuffs_jpeg__decoder__decode_mcu__choosy_default( goto label__dc_component__break; } label__dc_component__break:; + if (((uint64_t)(io2_v_r - iop_v_r)) < 264u) { + v_ret = 1u; + goto label__goto_done__break; + } v_ac_h = self->private_impl.f_mcu_blocks_ac_hselector[self->private_impl.f_mcu_current_block]; v_ac_huff_table_fast = &self->private_impl.f_huff_tables_fast[v_ac_h][0u]; while (true) { if (((uint64_t)(io2_v_r - iop_v_r)) < 8u) { - v_ret = 1u; + v_ret = 2u; goto label__goto_done__break; } v_bits |= (wuffs_base__peek_u64be__no_bounds_check(iop_v_r) >> (v_n_bits & 63u)); @@ -41837,11 +41841,15 @@ wuffs_jpeg__decoder__decode_mcu_progressive_ac_high_bits( ((uint64_t)(self->private_impl.f_bitstream_ri))); while (true) { while (true) { + if (((uint64_t)(io2_v_r - iop_v_r)) < 264u) { + v_ret = 1u; + goto label__goto_done__break; + } v_ac_h = self->private_impl.f_mcu_blocks_ac_hselector[0u]; v_ac_huff_table_fast = &self->private_impl.f_huff_tables_fast[v_ac_h][0u]; while (true) { if (((uint64_t)(io2_v_r - iop_v_r)) < 8u) { - v_ret = 1u; + v_ret = 2u; goto label__goto_done__break; } v_bits |= (wuffs_base__peek_u64be__no_bounds_check(iop_v_r) >> (v_n_bits & 63u)); diff --git a/std/jpeg/decode_jpeg.wuffs b/std/jpeg/decode_jpeg.wuffs index dbb4ce15..16448bd0 100644 --- a/std/jpeg/decode_jpeg.wuffs +++ b/std/jpeg/decode_jpeg.wuffs @@ -1514,11 +1514,11 @@ pri func decoder.fill_bitstream!(src: base.io_reader) { } endwhile // If we hit a "\xFF\x??" marker then pad the bitstream. In this method - // invocation, insert up to 32 bytes of implicit zeroes. Repeated calls to + // invocation, insert up to 264 bytes of implicit zeroes. Repeated calls to // this method can insert even more. if args.src.length() > 1 { if (args.src.peek_u8() >= 0xFF) and ((args.src.peek_u16le() >> 8) > 0) { - new_wi = wi.min(no_more_than: 0x800 - 32) + 32 + new_wi = wi.min(no_more_than: 0x800 - 264) + 264 if wi < new_wi { this.bitstream_buffer[wi .. new_wi].bulk_memset!(byte_value: 0) } diff --git a/std/jpeg/decode_mcu_default.wuffs b/std/jpeg/decode_mcu_default.wuffs index 9c45fba6..d0a1a185 100644 --- a/std/jpeg/decode_mcu_default.wuffs +++ b/std/jpeg/decode_mcu_default.wuffs @@ -133,6 +133,18 @@ pri func decoder.decode_mcu!(workbuf: slice base.u8, mx: base.u32[..= 0x1FFF], m break.dc_component } endwhile.dc_component + // Ensure that we have enough bits for the entire + // while.ac_components loop. + // + // Worst case, there are 63 AC components (round that up to 64) and + // each one needs (16 + 15) bits (round that up to 4 bytes), so we + // need (64 * 4) = 256 bytes of buffer. Add 8 bytes slack so that + // we can always call peek_u64be. + if r.length() < 264 { + ret = 1 // Request another fill_bitstream call. + break.goto_done + } + ac_h = this.mcu_blocks_ac_hselector[this.mcu_current_block] ac_huff_table_fast = this.huff_tables_fast[ac_h][..] as ptr array[256] base.u16 while.ac_components true, @@ -140,8 +152,10 @@ pri func decoder.decode_mcu!(workbuf: slice base.u8, mx: base.u32[..= 0x1FFF], m inv ac_huff_table_fast <> nullptr, { // Load at least 31 bits, by keeping 56 .. 64 loaded. + // + // TODO: eliminate this bounds check entirely. if r.length() < 8 { - ret = 1 // Request another fill_bitstream call. + ret = 2 // Internal error. break.goto_done } bits |= r.peek_u64be() >> (n_bits & 63) diff --git a/std/jpeg/decode_mcu_progressive_ac_high_bits.wuffs b/std/jpeg/decode_mcu_progressive_ac_high_bits.wuffs index 2510c70e..3beae2a4 100644 --- a/std/jpeg/decode_mcu_progressive_ac_high_bits.wuffs +++ b/std/jpeg/decode_mcu_progressive_ac_high_bits.wuffs @@ -52,14 +52,28 @@ pri func decoder.decode_mcu_progressive_ac_high_bits!(workbuf: slice base.u8, mx while.goto_done true {{ while.block true { + // Ensure that we have enough bits for the entire + // while.ac_components loop. + // + // Worst case, there are 63 AC components (round that up to 64) and + // each one needs (16 + 15) bits (round that up to 4 bytes), so we + // need (64 * 4) = 256 bytes of buffer. Add 8 bytes slack so that + // we can always call peek_u64be. + if r.length() < 264 { + ret = 1 // Request another fill_bitstream call. + break.goto_done + } + ac_h = this.mcu_blocks_ac_hselector[0] ac_huff_table_fast = this.huff_tables_fast[ac_h][..] as ptr array[256] base.u16 while.ac_components true, inv ac_huff_table_fast <> nullptr, { // Load at least 31 bits, by keeping 56 .. 64 loaded. + // + // TODO: eliminate this bounds check entirely. if r.length() < 8 { - ret = 1 // Request another fill_bitstream call. + ret = 2 // Internal error. break.goto_done } bits |= r.peek_u64be() >> (n_bits & 63) diff --git a/std/jpeg/decode_mcu_progressive_ac_low_bit.wuffs b/std/jpeg/decode_mcu_progressive_ac_low_bit.wuffs index 2f030222..82cf4767 100644 --- a/std/jpeg/decode_mcu_progressive_ac_low_bit.wuffs +++ b/std/jpeg/decode_mcu_progressive_ac_low_bit.wuffs @@ -68,7 +68,9 @@ pri func decoder.decode_mcu_progressive_ac_low_bit!(workbuf: slice base.u8, mx: // and processing the ac_value (up to 15 bits). // // After that, it's simpler for this "AC low bit" flavor if we - // never suspend for the rest of the while.ac_components loop. + // never suspend for the rest of this iteration of the + // while.ac_components loop. + // // Specifically, we want to have enough bits in the bitstream // so that we don't suspend inside the while.refine_non_zeroes // loop. In the worst case, we need another 8 bytes for