From 1878dc9037915f9906553caf550475f6f0998bfa Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Mon, 14 Dec 2020 09:46:34 +1100 Subject: [PATCH] Have std/bmp detect bad RLE compression --- release/c/wuffs-unsupported-snapshot.c | 10 ++++++++++ std/bmp/decode_bmp.wuffs | 7 +++++++ 2 files changed, 17 insertions(+) diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index 4cc5d6ea..00194483 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -5445,6 +5445,7 @@ extern "C" { // ---------------- Status Codes extern const char wuffs_bmp__error__bad_header[]; +extern const char wuffs_bmp__error__bad_rle_compression[]; extern const char wuffs_bmp__error__unsupported_bmp_file[]; // ---------------- Public Consts @@ -16110,6 +16111,7 @@ wuffs_adler32__hasher__update_u32( // ---------------- Status Codes Implementations const char wuffs_bmp__error__bad_header[] = "#bmp: bad header"; +const char wuffs_bmp__error__bad_rle_compression[] = "#bmp: bad RLE compression"; const char wuffs_bmp__error__unsupported_bmp_file[] = "#bmp: unsupported BMP file"; const char wuffs_bmp__note__internal_note_short_read[] = "@bmp: internal note: short read"; @@ -17818,6 +17820,10 @@ wuffs_bmp__decoder__swizzle_rle( wuffs_base__pixel_swizzler__swizzle_interleaved_transparent_black(&self->private_impl.f_swizzler, v_dst, v_dst_palette, ((uint64_t)(self->private_impl.f_rle_delta_x))); wuffs_base__u32__sat_add_indirect(&self->private_impl.f_dst_x, ((uint32_t)(self->private_impl.f_rle_delta_x))); self->private_impl.f_rle_delta_x = 0; + if (self->private_impl.f_dst_x > self->private_impl.f_width) { + status = wuffs_base__make_status(wuffs_bmp__error__bad_rle_compression); + goto exit; + } } if (v_code > 0) { #if defined(__GNUC__) @@ -17830,6 +17836,10 @@ wuffs_bmp__decoder__swizzle_rle( #endif while (true) { self->private_impl.f_dst_y += self->private_impl.f_dst_y_inc; + if (self->private_impl.f_dst_y == self->private_impl.f_dst_y_end) { + status = wuffs_base__make_status(wuffs_bmp__error__bad_rle_compression); + goto exit; + } v_row = wuffs_base__table_u8__row(v_tab, self->private_impl.f_dst_y); if (v_dst_bytes_per_row < ((uint64_t)(v_row.len))) { v_row = wuffs_base__slice_u8__subslice_j(v_row, v_dst_bytes_per_row); diff --git a/std/bmp/decode_bmp.wuffs b/std/bmp/decode_bmp.wuffs index eb9eab97..50d06221 100644 --- a/std/bmp/decode_bmp.wuffs +++ b/std/bmp/decode_bmp.wuffs @@ -13,6 +13,7 @@ // limitations under the License. pub status "#bad header" +pub status "#bad RLE compression" pub status "#unsupported BMP file" pri status "@internal note: short read" @@ -733,6 +734,9 @@ pri func decoder.swizzle_rle!(dst: ptr base.pixel_buffer, src: base.io_reader) b num_pixels: this.rle_delta_x as base.u64) this.dst_x ~sat+= this.rle_delta_x as base.u32 this.rle_delta_x = 0 + if this.dst_x > this.width { + return "#bad RLE compression" + } } // Paint the skipped rows (and the partial final row) with @@ -741,6 +745,9 @@ pri func decoder.swizzle_rle!(dst: ptr base.pixel_buffer, src: base.io_reader) b code -= 1 while true { this.dst_y ~mod+= this.dst_y_inc + if this.dst_y == this.dst_y_end { + return "#bad RLE compression" + } row = tab.row(y: this.dst_y) if dst_bytes_per_row < row.length() { row = row[.. dst_bytes_per_row]