diff --git a/doc/changelog.md b/doc/changelog.md index 83723aaa..9f4381da 100644 --- a/doc/changelog.md +++ b/doc/changelog.md @@ -6,6 +6,8 @@ - Changed `lzw.set_literal_width` to `lzw.set_quirk`. - Changed `set_quirk_enabled!(quirk: u32, enabled: bool)` to `set_quirk!(key: u32, value: u64) status`. +- Set image decoder pixel width and height inclusive maximum to `0xFF_FFFF`, + down from `0x7FFF_FFFF`. The dot points below probably aren't of interest unless you're _writing_ Wuffs code (instead of writing C/C++ code that _uses_ Wuffs' standard library). diff --git a/doc/std/image-decoders.md b/doc/std/image-decoders.md index e4ae6653..3480061e 100644 --- a/doc/std/image-decoders.md +++ b/doc/std/image-decoders.md @@ -7,12 +7,19 @@ but the `gif` could be replaced by `jpeg`, `png`, etc. Some image formats (GIF) are animated, consisting of an image header and then N frames. The image header gives e.g. the overall image's width and height. Each frame consists of a frame header (e.g. frame rectangle bounds, display -duration) and a payload (the pixels). +duration) and a payload (the pixels). Headers are also known as configurations. + +Wuffs image decoders will reject an image dimension (width or height) that is +above `0xFF_FFFF = 16_777215` pixels, even if the underlying image file format +permits more. This limit simplifies handling potential overflow. For example, +`width * bytes_per_pixel` will not overflow an `i32` and `width * height * +bytes_per_pixel` will not overflow an `i64`. Similarly, converting a pixel +width from integers to 26.6 fixed point units will not overflow an `i32`. In general, there is one `wuffs_base__image_config` and then N pairs of -(`wuffs_base__frame_config`, frame). Non-animated (still) image formats are -treated the same way: that their N is 1 and their single frame's bounds equals -the overall image bounds. +(`wuffs_base__frame_config`, frame payload). Non-animated (still) image formats +are treated the same way: that their N is 1 and their single frame's bounds +equals the overall image bounds. To decode everything (without knowing N in advance) sequentially: diff --git a/internal/cgen/base/image-public.h b/internal/cgen/base/image-public.h index 2c43a3b0..9f3e1dc2 100644 --- a/internal/cgen/base/image-public.h +++ b/internal/cgen/base/image-public.h @@ -14,6 +14,8 @@ // ---------------- Images +#define WUFFS_BASE__IMAGE__DIMENSION_MAX_INCL 0xFFFFFF + // wuffs_base__color_u32_argb_premul is an 8 bit per channel premultiplied // Alpha, Red, Green, Blue color, as a uint32_t value. Its value is always // 0xAARRGGBB (Alpha most significant, Blue least), regardless of endianness. diff --git a/lang/builtin/builtin.go b/lang/builtin/builtin.go index b907f842..37522eea 100644 --- a/lang/builtin/builtin.go +++ b/lang/builtin/builtin.go @@ -270,6 +270,7 @@ var Statuses = [...]string{ `"#no more information"`, `"#not enough data"`, `"#out of bounds"`, + `"#unsupported image dimension"`, `"#unsupported method"`, `"#unsupported option"`, `"#unsupported pixel swizzler option"`, @@ -612,7 +613,7 @@ var funcsOther = [...]string{ // ---- image_config - "image_config.set!(pixfmt: u32, pixsub: u32, width: u32, height: u32," + + "image_config.set!(pixfmt: u32, pixsub: u32, width: u32[..= 0xFF_FFFF], height: u32[..= 0xFF_FFFF]," + "first_frame_io_position: u64, first_frame_is_opaque: bool)", // ---- pixel_buffer diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index 3a9bc181..105b3519 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -451,6 +451,7 @@ extern const char wuffs_base__error__interleaved_coroutine_calls[]; extern const char wuffs_base__error__no_more_information[]; extern const char wuffs_base__error__not_enough_data[]; extern const char wuffs_base__error__out_of_bounds[]; +extern const char wuffs_base__error__unsupported_image_dimension[]; extern const char wuffs_base__error__unsupported_method[]; extern const char wuffs_base__error__unsupported_option[]; extern const char wuffs_base__error__unsupported_pixel_swizzler_option[]; @@ -3606,6 +3607,8 @@ wuffs_base__malloc_slice_u64(void* (*malloc_func)(size_t), uint64_t num_u64) { // ---------------- Images +#define WUFFS_BASE__IMAGE__DIMENSION_MAX_INCL 0xFFFFFF + // wuffs_base__color_u32_argb_premul is an 8 bit per channel premultiplied // Alpha, Red, Green, Blue color, as a uint32_t value. Its value is always // 0xAARRGGBB (Alpha most significant, Blue least), regardless of endianness. @@ -10413,7 +10416,7 @@ struct wuffs_wbmp__decoder__struct { struct { struct { uint32_t v_i; - uint32_t v_x32; + uint32_t v_p; } s_do_decode_image_config[1]; struct { uint64_t v_dst_bytes_per_pixel; @@ -12002,6 +12005,7 @@ const char wuffs_base__error__interleaved_coroutine_calls[] = "#base: interleave const char wuffs_base__error__no_more_information[] = "#base: no more information"; const char wuffs_base__error__not_enough_data[] = "#base: not enough data"; const char wuffs_base__error__out_of_bounds[] = "#base: out of bounds"; +const char wuffs_base__error__unsupported_image_dimension[] = "#base: unsupported image dimension"; const char wuffs_base__error__unsupported_method[] = "#base: unsupported method"; const char wuffs_base__error__unsupported_option[] = "#base: unsupported option"; const char wuffs_base__error__unsupported_pixel_swizzler_option[] = "#base: unsupported pixel swizzler option"; @@ -23072,9 +23076,12 @@ wuffs_bmp__decoder__do_decode_image_config( } v_width = t_7; } - if (v_width >= 2147483648) { + if (v_width > 2147483647) { status = wuffs_base__make_status(wuffs_bmp__error__bad_header); goto exit; + } else if (v_width > 16777215) { + status = wuffs_base__make_status(wuffs_base__error__unsupported_image_dimension); + goto exit; } self->private_impl.f_width = v_width; { @@ -23106,9 +23113,12 @@ wuffs_bmp__decoder__do_decode_image_config( } v_height = t_8; } - if (v_height >= 2147483648) { + if (v_height > 2147483647) { status = wuffs_base__make_status(wuffs_bmp__error__bad_header); goto exit; + } else if (v_height > 16777215) { + status = wuffs_base__make_status(wuffs_base__error__unsupported_image_dimension); + goto exit; } self->private_impl.f_height = v_height; { @@ -23203,9 +23213,12 @@ wuffs_bmp__decoder__do_decode_image_config( } v_width = t_11; } - if (v_width >= 2147483648) { + if (v_width > 2147483647) { status = wuffs_base__make_status(wuffs_bmp__error__bad_header); goto exit; + } else if (v_width > 16777215) { + status = wuffs_base__make_status(wuffs_base__error__unsupported_image_dimension); + goto exit; } self->private_impl.f_width = v_width; { @@ -23240,9 +23253,17 @@ wuffs_bmp__decoder__do_decode_image_config( if (v_height == 2147483648) { status = wuffs_base__make_status(wuffs_bmp__error__bad_header); goto exit; - } else if (v_height >= 2147483648) { - self->private_impl.f_height = (((uint32_t)(0 - v_height)) & 2147483647); + } else if (v_height > 2147483648) { + v_height = ((uint32_t)(0 - v_height)); + if (v_height > 16777215) { + status = wuffs_base__make_status(wuffs_base__error__unsupported_image_dimension); + goto exit; + } + self->private_impl.f_height = v_height; self->private_impl.f_top_down = true; + } else if (v_height > 16777215) { + status = wuffs_base__make_status(wuffs_base__error__unsupported_image_dimension); + goto exit; } else { self->private_impl.f_height = v_height; } @@ -24069,7 +24090,7 @@ wuffs_bmp__decoder__swizzle_none( wuffs_base__pixel_format v_dst_pixfmt = {0}; uint32_t v_dst_bits_per_pixel = 0; - uint64_t v_dst_bytes_per_pixel = 0; + uint32_t v_dst_bytes_per_pixel = 0; uint64_t v_dst_bytes_per_row = 0; uint32_t v_src_bytes_per_pixel = 0; wuffs_base__slice_u8 v_dst_palette = {0}; @@ -24096,8 +24117,8 @@ wuffs_bmp__decoder__swizzle_none( status = wuffs_base__make_status(wuffs_base__error__unsupported_option); goto exit; } - v_dst_bytes_per_pixel = ((uint64_t)((v_dst_bits_per_pixel / 8))); - v_dst_bytes_per_row = (((uint64_t)(self->private_impl.f_width)) * v_dst_bytes_per_pixel); + v_dst_bytes_per_pixel = (v_dst_bits_per_pixel / 8); + v_dst_bytes_per_row = ((uint64_t)((self->private_impl.f_width * v_dst_bytes_per_pixel))); v_dst_palette = wuffs_base__pixel_buffer__palette_or_else(a_dst, wuffs_base__make_slice_u8_ij(self->private_data.f_scratch, 1024, 2048)); v_tab = wuffs_base__pixel_buffer__plane(a_dst, 0); label__outer__continue:; @@ -24128,7 +24149,7 @@ wuffs_bmp__decoder__swizzle_none( if (v_dst_bytes_per_row < ((uint64_t)(v_dst.len))) { v_dst = wuffs_base__slice_u8__subslice_j(v_dst, v_dst_bytes_per_row); } - v_i = (((uint64_t)(self->private_impl.f_dst_x)) * v_dst_bytes_per_pixel); + v_i = (((uint64_t)(self->private_impl.f_dst_x)) * ((uint64_t)(v_dst_bytes_per_pixel))); if (v_i >= ((uint64_t)(v_dst.len))) { if (self->private_impl.f_bits_per_pixel > 32) { status = wuffs_base__make_status(wuffs_bmp__error__unsupported_bmp_file); @@ -24194,7 +24215,7 @@ wuffs_bmp__decoder__swizzle_rle( wuffs_base__pixel_format v_dst_pixfmt = {0}; uint32_t v_dst_bits_per_pixel = 0; - uint64_t v_dst_bytes_per_pixel = 0; + uint32_t v_dst_bytes_per_pixel = 0; uint64_t v_dst_bytes_per_row = 0; wuffs_base__slice_u8 v_dst_palette = {0}; wuffs_base__table_u8 v_tab = {0}; @@ -24226,8 +24247,8 @@ wuffs_bmp__decoder__swizzle_rle( status = wuffs_base__make_status(wuffs_base__error__unsupported_option); goto exit; } - v_dst_bytes_per_pixel = ((uint64_t)((v_dst_bits_per_pixel / 8))); - v_dst_bytes_per_row = (((uint64_t)(self->private_impl.f_width)) * v_dst_bytes_per_pixel); + v_dst_bytes_per_pixel = (v_dst_bits_per_pixel / 8); + v_dst_bytes_per_row = ((uint64_t)((self->private_impl.f_width * v_dst_bytes_per_pixel))); v_dst_palette = wuffs_base__pixel_buffer__palette_or_else(a_dst, wuffs_base__make_slice_u8_ij(self->private_data.f_scratch, 1024, 2048)); v_tab = wuffs_base__pixel_buffer__plane(a_dst, 0); v_rle_state = self->private_impl.f_rle_state; @@ -24239,7 +24260,7 @@ wuffs_bmp__decoder__swizzle_rle( } label__middle__continue:; while (true) { - v_i = (((uint64_t)(self->private_impl.f_dst_x)) * v_dst_bytes_per_pixel); + v_i = (((uint64_t)(self->private_impl.f_dst_x)) * ((uint64_t)(v_dst_bytes_per_pixel))); if (v_i <= ((uint64_t)(v_row.len))) { v_dst = wuffs_base__slice_u8__subslice_i(v_row, v_i); } else { @@ -24456,7 +24477,7 @@ wuffs_bmp__decoder__swizzle_bitfields( wuffs_base__pixel_format v_dst_pixfmt = {0}; uint32_t v_dst_bits_per_pixel = 0; - uint64_t v_dst_bytes_per_pixel = 0; + uint32_t v_dst_bytes_per_pixel = 0; uint64_t v_dst_bytes_per_row = 0; wuffs_base__slice_u8 v_dst_palette = {0}; wuffs_base__table_u8 v_tab = {0}; @@ -24488,8 +24509,8 @@ wuffs_bmp__decoder__swizzle_bitfields( status = wuffs_base__make_status(wuffs_base__error__unsupported_option); goto exit; } - v_dst_bytes_per_pixel = ((uint64_t)((v_dst_bits_per_pixel / 8))); - v_dst_bytes_per_row = (((uint64_t)(self->private_impl.f_width)) * v_dst_bytes_per_pixel); + v_dst_bytes_per_pixel = (v_dst_bits_per_pixel / 8); + v_dst_bytes_per_row = ((uint64_t)((self->private_impl.f_width * v_dst_bytes_per_pixel))); v_dst_palette = wuffs_base__pixel_buffer__palette_or_else(a_dst, wuffs_base__make_slice_u8_ij(self->private_data.f_scratch, 1024, 2048)); v_tab = wuffs_base__pixel_buffer__plane(a_dst, 0); label__outer__continue:; @@ -24558,7 +24579,7 @@ wuffs_bmp__decoder__swizzle_bitfields( if (v_dst_bytes_per_row < ((uint64_t)(v_dst.len))) { v_dst = wuffs_base__slice_u8__subslice_j(v_dst, v_dst_bytes_per_row); } - v_i = (((uint64_t)(self->private_impl.f_dst_x)) * v_dst_bytes_per_pixel); + v_i = (((uint64_t)(self->private_impl.f_dst_x)) * ((uint64_t)(v_dst_bytes_per_pixel))); if (v_i >= ((uint64_t)(v_dst.len))) { v_n = ((uint64_t)(v_p0)); } else { @@ -24596,7 +24617,7 @@ wuffs_bmp__decoder__swizzle_low_bit_depth( wuffs_base__pixel_format v_dst_pixfmt = {0}; uint32_t v_dst_bits_per_pixel = 0; - uint64_t v_dst_bytes_per_pixel = 0; + uint32_t v_dst_bytes_per_pixel = 0; uint64_t v_dst_bytes_per_row = 0; wuffs_base__slice_u8 v_dst_palette = {0}; wuffs_base__table_u8 v_tab = {0}; @@ -24625,8 +24646,8 @@ wuffs_bmp__decoder__swizzle_low_bit_depth( status = wuffs_base__make_status(wuffs_base__error__unsupported_option); goto exit; } - v_dst_bytes_per_pixel = ((uint64_t)((v_dst_bits_per_pixel / 8))); - v_dst_bytes_per_row = (((uint64_t)(self->private_impl.f_width)) * v_dst_bytes_per_pixel); + v_dst_bytes_per_pixel = (v_dst_bits_per_pixel / 8); + v_dst_bytes_per_row = ((uint64_t)((self->private_impl.f_width * v_dst_bytes_per_pixel))); v_dst_palette = wuffs_base__pixel_buffer__palette_or_else(a_dst, wuffs_base__make_slice_u8_ij(self->private_data.f_scratch, 1024, 2048)); v_tab = wuffs_base__pixel_buffer__plane(a_dst, 0); label__loop__continue:; @@ -24642,7 +24663,7 @@ wuffs_bmp__decoder__swizzle_low_bit_depth( if (v_dst_bytes_per_row < ((uint64_t)(v_dst.len))) { v_dst = wuffs_base__slice_u8__subslice_j(v_dst, v_dst_bytes_per_row); } - v_i = (((uint64_t)(self->private_impl.f_dst_x)) * v_dst_bytes_per_pixel); + v_i = (((uint64_t)(self->private_impl.f_dst_x)) * ((uint64_t)(v_dst_bytes_per_pixel))); if (v_i >= ((uint64_t)(v_dst.len))) { if (self->private_impl.f_bits_per_pixel == 1) { v_chunk_count = ((wuffs_base__u32__sat_sub(self->private_impl.f_width, self->private_impl.f_dst_x) + 31) / 32); @@ -34266,7 +34287,7 @@ wuffs_gif__decoder__copy_to_image_buffer( return wuffs_base__make_status(wuffs_base__error__unsupported_option); } v_bytes_per_pixel = (v_bits_per_pixel >> 3); - v_width_in_bytes = (((uint64_t)(self->private_impl.f_width)) * ((uint64_t)(v_bytes_per_pixel))); + v_width_in_bytes = ((uint64_t)((self->private_impl.f_width * v_bytes_per_pixel))); v_tab = wuffs_base__pixel_buffer__plane(a_pb, 0); label__0__continue:; while (v_src_ri < ((uint64_t)(a_src.len))) { @@ -37491,9 +37512,12 @@ wuffs_nie__decoder__do_decode_image_config( } v_a = t_2; } - if (v_a >= 2147483648) { + if (v_a > 2147483647) { status = wuffs_base__make_status(wuffs_nie__error__bad_header); goto exit; + } else if (v_a > 16777215) { + status = wuffs_base__make_status(wuffs_base__error__unsupported_image_dimension); + goto exit; } self->private_impl.f_width = v_a; { @@ -37525,9 +37549,12 @@ wuffs_nie__decoder__do_decode_image_config( } v_a = t_3; } - if (v_a >= 2147483648) { + if (v_a > 2147483647) { status = wuffs_base__make_status(wuffs_nie__error__bad_header); goto exit; + } else if (v_a > 16777215) { + status = wuffs_base__make_status(wuffs_base__error__unsupported_image_dimension); + goto exit; } self->private_impl.f_height = v_a; if (a_dst != NULL) { @@ -37877,7 +37904,7 @@ wuffs_nie__decoder__swizzle( wuffs_base__pixel_format v_dst_pixfmt = {0}; uint32_t v_dst_bits_per_pixel = 0; - uint64_t v_dst_bytes_per_pixel = 0; + uint32_t v_dst_bytes_per_pixel = 0; uint64_t v_dst_bytes_per_row = 0; uint32_t v_src_bytes_per_pixel = 0; wuffs_base__table_u8 v_tab = {0}; @@ -37903,8 +37930,8 @@ wuffs_nie__decoder__swizzle( status = wuffs_base__make_status(wuffs_base__error__unsupported_option); goto exit; } - v_dst_bytes_per_pixel = ((uint64_t)((v_dst_bits_per_pixel / 8))); - v_dst_bytes_per_row = (((uint64_t)(self->private_impl.f_width)) * v_dst_bytes_per_pixel); + v_dst_bytes_per_pixel = (v_dst_bits_per_pixel / 8); + v_dst_bytes_per_row = ((uint64_t)((self->private_impl.f_width * v_dst_bytes_per_pixel))); v_tab = wuffs_base__pixel_buffer__plane(a_dst, 0); while (true) { if (self->private_impl.f_dst_x == self->private_impl.f_width) { @@ -37918,7 +37945,7 @@ wuffs_nie__decoder__swizzle( if (v_dst_bytes_per_row < ((uint64_t)(v_dst.len))) { v_dst = wuffs_base__slice_u8__subslice_j(v_dst, v_dst_bytes_per_row); } - v_i = (((uint64_t)(self->private_impl.f_dst_x)) * v_dst_bytes_per_pixel); + v_i = (((uint64_t)(self->private_impl.f_dst_x)) * ((uint64_t)(v_dst_bytes_per_pixel))); if (v_i >= ((uint64_t)(v_dst.len))) { v_src_bytes_per_pixel = 4; if (self->private_impl.f_pixfmt == 2164308923) { @@ -41005,11 +41032,11 @@ wuffs_png__decoder__decode_ihdr( } v_a32 = t_0; } - if ((v_a32 == 0) || (v_a32 >= 2147483648)) { + if ((v_a32 == 0) || (v_a32 > 2147483647)) { status = wuffs_base__make_status(wuffs_png__error__bad_header); goto exit; - } else if (v_a32 >= 16777216) { - status = wuffs_base__make_status(wuffs_png__error__unsupported_png_file); + } else if (v_a32 > 16777215) { + status = wuffs_base__make_status(wuffs_base__error__unsupported_image_dimension); goto exit; } self->private_impl.f_width = v_a32; @@ -41042,11 +41069,11 @@ wuffs_png__decoder__decode_ihdr( } v_a32 = t_1; } - if ((v_a32 == 0) || (v_a32 >= 2147483648)) { + if ((v_a32 == 0) || (v_a32 > 2147483647)) { status = wuffs_base__make_status(wuffs_png__error__bad_header); goto exit; - } else if (v_a32 >= 16777216) { - status = wuffs_base__make_status(wuffs_png__error__unsupported_png_file); + } else if (v_a32 > 16777215) { + status = wuffs_base__make_status(wuffs_base__error__unsupported_image_dimension); goto exit; } self->private_impl.f_height = v_a32; @@ -46721,8 +46748,7 @@ wuffs_wbmp__decoder__do_decode_image_config( uint8_t v_c = 0; uint32_t v_i = 0; - uint32_t v_x32 = 0; - uint64_t v_x64 = 0; + uint32_t v_p = 0; const uint8_t* iop_a_src = NULL; const uint8_t* io0_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL; @@ -46738,7 +46764,7 @@ wuffs_wbmp__decoder__do_decode_image_config( uint32_t coro_susp_point = self->private_impl.p_do_decode_image_config[0]; if (coro_susp_point) { v_i = self->private_data.s_do_decode_image_config[0].v_i; - v_x32 = self->private_data.s_do_decode_image_config[0].v_x32; + v_p = self->private_data.s_do_decode_image_config[0].v_p; } switch (coro_susp_point) { WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; @@ -46766,7 +46792,7 @@ wuffs_wbmp__decoder__do_decode_image_config( } v_i = 0; while (v_i < 2) { - v_x32 = 0; + v_p = 0; while (true) { { WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2); @@ -46777,22 +46803,20 @@ wuffs_wbmp__decoder__do_decode_image_config( uint8_t t_1 = *iop_a_src++; v_c = t_1; } - v_x32 |= ((uint32_t)((v_c & 127))); + v_p |= ((uint32_t)((v_c & 127))); if ((v_c >> 7) == 0) { goto label__0__break; - } - v_x64 = (((uint64_t)(v_x32)) << 7); - if (v_x64 > 4294967295) { - status = wuffs_base__make_status(wuffs_wbmp__error__bad_header); + } else if (v_p > 131071) { + status = wuffs_base__make_status(wuffs_base__error__unsupported_image_dimension); goto exit; } - v_x32 = ((uint32_t)(v_x64)); + v_p <<= 7; } label__0__break:; if (v_i == 0) { - self->private_impl.f_width = v_x32; + self->private_impl.f_width = v_p; } else { - self->private_impl.f_height = v_x32; + self->private_impl.f_height = v_p; } v_i += 1; } @@ -46819,7 +46843,7 @@ wuffs_wbmp__decoder__do_decode_image_config( suspend: self->private_impl.p_do_decode_image_config[0] = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0; self->private_data.s_do_decode_image_config[0].v_i = v_i; - self->private_data.s_do_decode_image_config[0].v_x32 = v_x32; + self->private_data.s_do_decode_image_config[0].v_p = v_p; goto exit; exit: diff --git a/std/bmp/decode_bmp.wuffs b/std/bmp/decode_bmp.wuffs index db6f86bc..c071ef8a 100644 --- a/std/bmp/decode_bmp.wuffs +++ b/std/bmp/decode_bmp.wuffs @@ -31,8 +31,8 @@ pri const COMPRESSION_ALPHABITFIELDS : base.u32 = 6 pri const COMPRESSION_LOW_BIT_DEPTH : base.u32 = 0x100 pub struct decoder? implements base.image_decoder( - width : base.u32[..= 0x7FFF_FFFF], - height : base.u32[..= 0x7FFF_FFFF], + width : base.u32[..= 0xFF_FFFF], + height : base.u32[..= 0xFF_FFFF], // The call sequence state machine is discussed in // (/doc/std/image-decoders-call-sequence.md). @@ -131,6 +131,10 @@ pri func decoder.do_decode_image_config?(dst: nptr base.image_config, src: base. // Read the BITMAPINFOHEADER: // - OS/2 is 12, 16 or 64 bytes. // - Windows is 40, 108 or 124 bytes. + // + // https://learn.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-bitmapinfoheader + // says that width and height are LONG (signed i32) not DWORD (unsigned + // u32) and hence values above 0x7FFF_FFFF are invalid. this.bitmap_info_len = args.src.read_u32le?() if this.padding < this.bitmap_info_len { @@ -149,13 +153,17 @@ pri func decoder.do_decode_image_config?(dst: nptr base.image_config, src: base. } else if this.bitmap_info_len == 16 { width = args.src.read_u32le?() - if width >= 0x8000_0000 { + if width > 0x7FFF_FFFF { return "#bad header" + } else if width > 0xFF_FFFF { + return base."#unsupported image dimension" } this.width = width height = args.src.read_u32le?() - if height >= 0x8000_0000 { + if height > 0x7FFF_FFFF { return "#bad header" + } else if height > 0xFF_FFFF { + return base."#unsupported image dimension" } this.height = height planes = args.src.read_u16le_as_u32?() @@ -166,19 +174,25 @@ pri func decoder.do_decode_image_config?(dst: nptr base.image_config, src: base. } else { width = args.src.read_u32le?() - if width >= 0x8000_0000 { + if width > 0x7FFF_FFFF { return "#bad header" + } else if width > 0xFF_FFFF { + return base."#unsupported image dimension" } this.width = width height = args.src.read_u32le?() if height == 0x8000_0000 { return "#bad header" - } else if height >= 0x8000_0000 { - // The &0x7FFF_FFFF is redundant, but proves to the compiler that - // the result is within this.height's refined bounds. - this.height = (0 ~mod- height) & 0x7FFF_FFFF + } else if height > 0x8000_0000 { + height = 0 ~mod- height + if height > 0xFF_FFFF { + return base."#unsupported image dimension" + } + this.height = height this.top_down = true + } else if height > 0xFF_FFFF { + return base."#unsupported image dimension" } else { this.height = height } @@ -503,7 +517,7 @@ pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reade pri func decoder.swizzle_none!(dst: ptr base.pixel_buffer, src: base.io_reader) base.status { var dst_pixfmt : base.pixel_format var dst_bits_per_pixel : base.u32[..= 256] - var dst_bytes_per_pixel : base.u64[..= 32] + var dst_bytes_per_pixel : base.u32[..= 32] var dst_bytes_per_row : base.u64 var src_bytes_per_pixel : base.u32[..= 4] var dst_palette : slice base.u8 @@ -520,8 +534,8 @@ pri func decoder.swizzle_none!(dst: ptr base.pixel_buffer, src: base.io_reader) if (dst_bits_per_pixel & 7) <> 0 { return base."#unsupported option" } - dst_bytes_per_pixel = (dst_bits_per_pixel / 8) as base.u64 - dst_bytes_per_row = (this.width as base.u64) * dst_bytes_per_pixel + dst_bytes_per_pixel = dst_bits_per_pixel / 8 + dst_bytes_per_row = (this.width * dst_bytes_per_pixel) as base.u64 dst_palette = args.dst.palette_or_else(fallback: this.scratch[1024 ..]) tab = args.dst.plane(p: 0) @@ -553,7 +567,7 @@ pri func decoder.swizzle_none!(dst: ptr base.pixel_buffer, src: base.io_reader) if dst_bytes_per_row < dst.length() { dst = dst[.. dst_bytes_per_row] } - i = (this.dst_x as base.u64) * dst_bytes_per_pixel + i = (this.dst_x as base.u64) * (dst_bytes_per_pixel as base.u64) if i >= dst.length() { if this.bits_per_pixel > 32 { return "#unsupported BMP file" @@ -607,7 +621,7 @@ pri const RLE_STATE_DELTA_Y : base.u32 = 5 pri func decoder.swizzle_rle!(dst: ptr base.pixel_buffer, src: base.io_reader) base.status { var dst_pixfmt : base.pixel_format var dst_bits_per_pixel : base.u32[..= 256] - var dst_bytes_per_pixel : base.u64[..= 32] + var dst_bytes_per_pixel : base.u32[..= 32] var dst_bytes_per_row : base.u64 var dst_palette : slice base.u8 var tab : table base.u8 @@ -632,8 +646,8 @@ pri func decoder.swizzle_rle!(dst: ptr base.pixel_buffer, src: base.io_reader) b if (dst_bits_per_pixel & 7) <> 0 { return base."#unsupported option" } - dst_bytes_per_pixel = (dst_bits_per_pixel / 8) as base.u64 - dst_bytes_per_row = (this.width as base.u64) * dst_bytes_per_pixel + dst_bytes_per_pixel = dst_bits_per_pixel / 8 + dst_bytes_per_row = (this.width * dst_bytes_per_pixel) as base.u64 dst_palette = args.dst.palette_or_else(fallback: this.scratch[1024 ..]) tab = args.dst.plane(p: 0) @@ -646,7 +660,7 @@ pri func decoder.swizzle_rle!(dst: ptr base.pixel_buffer, src: base.io_reader) b } while.middle true { - i = (this.dst_x as base.u64) * dst_bytes_per_pixel + i = (this.dst_x as base.u64) * (dst_bytes_per_pixel as base.u64) if i <= row.length() { dst = row[i ..] } else { @@ -861,7 +875,7 @@ pri func decoder.swizzle_rle!(dst: ptr base.pixel_buffer, src: base.io_reader) b pri func decoder.swizzle_bitfields!(dst: ptr base.pixel_buffer, src: base.io_reader) base.status { var dst_pixfmt : base.pixel_format var dst_bits_per_pixel : base.u32[..= 256] - var dst_bytes_per_pixel : base.u64[..= 32] + var dst_bytes_per_pixel : base.u32[..= 32] var dst_bytes_per_row : base.u64 var dst_palette : slice base.u8 var tab : table base.u8 @@ -885,8 +899,8 @@ pri func decoder.swizzle_bitfields!(dst: ptr base.pixel_buffer, src: base.io_rea if (dst_bits_per_pixel & 7) <> 0 { return base."#unsupported option" } - dst_bytes_per_pixel = (dst_bits_per_pixel / 8) as base.u64 - dst_bytes_per_row = (this.width as base.u64) * dst_bytes_per_pixel + dst_bytes_per_pixel = dst_bits_per_pixel / 8 + dst_bytes_per_row = (this.width * dst_bytes_per_pixel) as base.u64 dst_palette = args.dst.palette_or_else(fallback: this.scratch[1024 ..]) tab = args.dst.plane(p: 0) @@ -968,7 +982,7 @@ pri func decoder.swizzle_bitfields!(dst: ptr base.pixel_buffer, src: base.io_rea if dst_bytes_per_row < dst.length() { dst = dst[.. dst_bytes_per_row] } - i = (this.dst_x as base.u64) * dst_bytes_per_pixel + i = (this.dst_x as base.u64) * (dst_bytes_per_pixel as base.u64) if i >= dst.length() { n = p0 as base.u64 } else { @@ -990,7 +1004,7 @@ pri func decoder.swizzle_bitfields!(dst: ptr base.pixel_buffer, src: base.io_rea pri func decoder.swizzle_low_bit_depth!(dst: ptr base.pixel_buffer, src: base.io_reader) base.status { var dst_pixfmt : base.pixel_format var dst_bits_per_pixel : base.u32[..= 256] - var dst_bytes_per_pixel : base.u64[..= 32] + var dst_bytes_per_pixel : base.u32[..= 32] var dst_bytes_per_row : base.u64 var dst_palette : slice base.u8 var tab : table base.u8 @@ -1011,8 +1025,8 @@ pri func decoder.swizzle_low_bit_depth!(dst: ptr base.pixel_buffer, src: base.io if (dst_bits_per_pixel & 7) <> 0 { return base."#unsupported option" } - dst_bytes_per_pixel = (dst_bits_per_pixel / 8) as base.u64 - dst_bytes_per_row = (this.width as base.u64) * dst_bytes_per_pixel + dst_bytes_per_pixel = dst_bits_per_pixel / 8 + dst_bytes_per_row = (this.width * dst_bytes_per_pixel) as base.u64 dst_palette = args.dst.palette_or_else(fallback: this.scratch[1024 ..]) tab = args.dst.plane(p: 0) @@ -1029,7 +1043,7 @@ pri func decoder.swizzle_low_bit_depth!(dst: ptr base.pixel_buffer, src: base.io if dst_bytes_per_row < dst.length() { dst = dst[.. dst_bytes_per_row] } - i = (this.dst_x as base.u64) * dst_bytes_per_pixel + i = (this.dst_x as base.u64) * (dst_bytes_per_pixel as base.u64) if i >= dst.length() { if this.bits_per_pixel == 1 { chunk_count = ((this.width ~sat- this.dst_x) + 31) / 32 diff --git a/std/gif/decode_gif.wuffs b/std/gif/decode_gif.wuffs index 74600feb..d671e4d0 100644 --- a/std/gif/decode_gif.wuffs +++ b/std/gif/decode_gif.wuffs @@ -13,8 +13,11 @@ // limitations under the License. pub struct decoder? implements base.image_decoder( - width : base.u32, - height : base.u32, + // These upper bounds should be 0xFFFF, on a strict reading of the GIF + // spec, but our looser implementation uses 0x1_FFFE. See + // test/data/artificial-gif/frame-out-of-bounds.gif.make-artificial.txt + width : base.u32[..= 0x1_FFFE], + height : base.u32[..= 0x1_FFFE], // The call sequence state machine is discussed in // (/doc/std/image-decoders-call-sequence.md). @@ -60,10 +63,10 @@ pub struct decoder? implements base.image_decoder( num_decoded_frame_configs_value : base.u64, num_decoded_frames_value : base.u64, - frame_rect_x0 : base.u32, - frame_rect_y0 : base.u32, - frame_rect_x1 : base.u32, - frame_rect_y1 : base.u32, + frame_rect_x0 : base.u32[..= 0x0_FFFF], + frame_rect_y0 : base.u32[..= 0x0_FFFF], + frame_rect_x1 : base.u32[..= 0x1_FFFE], + frame_rect_y1 : base.u32[..= 0x1_FFFE], // The dst_etc fields are the output cursor during copy_to_image_buffer. dst_x : base.u32, @@ -788,9 +791,9 @@ pri func decoder.decode_id_part0?(src: base.io_reader) { this.frame_rect_x0 = args.src.read_u16le_as_u32?() this.frame_rect_y0 = args.src.read_u16le_as_u32?() this.frame_rect_x1 = args.src.read_u16le_as_u32?() - this.frame_rect_x1 ~mod+= this.frame_rect_x0 + this.frame_rect_x1 += this.frame_rect_x0 this.frame_rect_y1 = args.src.read_u16le_as_u32?() - this.frame_rect_y1 ~mod+= this.frame_rect_y0 + this.frame_rect_y1 += this.frame_rect_y0 this.dst_x = this.frame_rect_x0 this.dst_y = this.frame_rect_y0 @@ -1039,7 +1042,7 @@ pri func decoder.copy_to_image_buffer!(pb: ptr base.pixel_buffer, src: slice bas } bytes_per_pixel = bits_per_pixel >> 3 - width_in_bytes = (this.width as base.u64) * (bytes_per_pixel as base.u64) + width_in_bytes = (this.width * bytes_per_pixel) as base.u64 tab = args.pb.plane(p: 0) while src_ri < args.src.length() { src = args.src[src_ri ..] diff --git a/std/nie/decode_nie.wuffs b/std/nie/decode_nie.wuffs index f231e805..619369aa 100644 --- a/std/nie/decode_nie.wuffs +++ b/std/nie/decode_nie.wuffs @@ -22,8 +22,8 @@ pub const DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE : base.u64 = 0 pub struct decoder? implements base.image_decoder( pixfmt : base.u32, - width : base.u32[..= 0x7FFF_FFFF], - height : base.u32[..= 0x7FFF_FFFF], + width : base.u32[..= 0xFF_FFFF], + height : base.u32[..= 0xFF_FFFF], // The call sequence state machine is discussed in // (/doc/std/image-decoders-call-sequence.md). @@ -78,14 +78,18 @@ pri func decoder.do_decode_image_config?(dst: nptr base.image_config, src: base. } a = args.src.read_u32le?() - if a >= 0x8000_0000 { + if a > 0x7FFF_FFFF { return "#bad header" + } else if a > 0xFF_FFFF { + return base."#unsupported image dimension" } this.width = a a = args.src.read_u32le?() - if a >= 0x8000_0000 { + if a > 0x7FFF_FFFF { return "#bad header" + } else if a > 0xFF_FFFF { + return base."#unsupported image dimension" } this.height = a @@ -200,7 +204,7 @@ pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reade pri func decoder.swizzle!(dst: ptr base.pixel_buffer, src: base.io_reader) base.status { var dst_pixfmt : base.pixel_format var dst_bits_per_pixel : base.u32[..= 256] - var dst_bytes_per_pixel : base.u64[..= 32] + var dst_bytes_per_pixel : base.u32[..= 32] var dst_bytes_per_row : base.u64 var src_bytes_per_pixel : base.u32[..= 8] var tab : table base.u8 @@ -216,8 +220,8 @@ pri func decoder.swizzle!(dst: ptr base.pixel_buffer, src: base.io_reader) base. if (dst_bits_per_pixel & 7) <> 0 { return base."#unsupported option" } - dst_bytes_per_pixel = (dst_bits_per_pixel / 8) as base.u64 - dst_bytes_per_row = (this.width as base.u64) * dst_bytes_per_pixel + dst_bytes_per_pixel = dst_bits_per_pixel / 8 + dst_bytes_per_row = (this.width * dst_bytes_per_pixel) as base.u64 tab = args.dst.plane(p: 0) while true { @@ -233,7 +237,7 @@ pri func decoder.swizzle!(dst: ptr base.pixel_buffer, src: base.io_reader) base. if dst_bytes_per_row < dst.length() { dst = dst[.. dst_bytes_per_row] } - i = (this.dst_x as base.u64) * dst_bytes_per_pixel + i = (this.dst_x as base.u64) * (dst_bytes_per_pixel as base.u64) if i >= dst.length() { src_bytes_per_pixel = 4 assert src_bytes_per_pixel > 0 diff --git a/std/png/decode_png.wuffs b/std/png/decode_png.wuffs index 216ca836..1eedfa24 100644 --- a/std/png/decode_png.wuffs +++ b/std/png/decode_png.wuffs @@ -16,8 +16,6 @@ use "std/crc32" use "std/zlib" pub struct decoder? implements base.image_decoder( - // The 0x00FF_FFFF limit is arbitrary (the PNG spec says 0x7FFF_FFFF) but - // it means that (width * height * bytes_per_pixel) doesn't overflow a u64. width : base.u32[..= 0x00FF_FFFF], height : base.u32[..= 0x00FF_FFFF], @@ -336,18 +334,18 @@ pri func decoder.decode_ihdr?(src: base.io_reader) { var a8 : base.u8 a32 = args.src.read_u32be?() - if (a32 == 0) or (a32 >= 0x8000_0000) { + if (a32 == 0) or (a32 > 0x7FFF_FFFF) { return "#bad header" - } else if a32 >= 0x0100_0000 { - return "#unsupported PNG file" + } else if a32 > 0xFF_FFFF { + return base."#unsupported image dimension" } this.width = a32 a32 = args.src.read_u32be?() - if (a32 == 0) or (a32 >= 0x8000_0000) { + if (a32 == 0) or (a32 > 0x7FFF_FFFF) { return "#bad header" - } else if a32 >= 0x0100_0000 { - return "#unsupported PNG file" + } else if a32 > 0xFF_FFFF { + return base."#unsupported image dimension" } this.height = a32 diff --git a/std/wbmp/decode_wbmp.wuffs b/std/wbmp/decode_wbmp.wuffs index 1fa78c72..a90686cb 100644 --- a/std/wbmp/decode_wbmp.wuffs +++ b/std/wbmp/decode_wbmp.wuffs @@ -18,8 +18,8 @@ pub status "#truncated input" pub const DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE : base.u64 = 0 pub struct decoder? implements base.image_decoder( - width : base.u32, - height : base.u32, + width : base.u32[..= 0xFF_FFFF], + height : base.u32[..= 0xFF_FFFF], // The call sequence state machine is discussed in // (/doc/std/image-decoders-call-sequence.md). @@ -48,10 +48,9 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_ } pri func decoder.do_decode_image_config?(dst: nptr base.image_config, src: base.io_reader) { - var c : base.u8 - var i : base.u32 - var x32 : base.u32 - var x64 : base.u64 + var c : base.u8 + var i : base.u32 + var p : base.u32[..= 0xFF_FFFF] if this.call_sequence <> 0x00 { return base."#bad call sequence" @@ -70,27 +69,25 @@ pri func decoder.do_decode_image_config?(dst: nptr base.image_config, src: base. // Width, height. i = 0 while i < 2 { - x32 = 0 + p = 0 while true, inv i < 2, { c = args.src.read_u8?() - x32 |= (c & 0x7F) as base.u32 + p |= (c & 0x7F) as base.u32 if (c >> 7) == 0 { break + } else if p > 0x1_FFFF { + return base."#unsupported image dimension" } - x64 = (x32 as base.u64) << 7 - if x64 > 0xFFFF_FFFF { - return "#bad header" - } - x32 = x64 as base.u32 + p <<= 7 } endwhile if i == 0 { - this.width = x32 + this.width = p } else { - this.height = x32 + this.height = p } i += 1 } endwhile @@ -212,19 +209,19 @@ pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reade if this.width > 0 { tab = args.dst.plane(p: 0) while dst_y < this.height { - assert dst_y < 0xFFFF_FFFF via "a < b: a < c; c <= b"(c: this.height) + assert dst_y < 0xFF_FFFF via "a < b: a < c; c <= b"(c: this.height) dst = tab.row_u32(y: dst_y) dst_x = 0 while dst_x < this.width, - inv dst_y < 0xFFFF_FFFF, + inv dst_y < 0xFF_FFFF, { - assert dst_x < 0xFFFF_FFFF via "a < b: a < c; c <= b"(c: this.width) + assert dst_x < 0xFF_FFFF via "a < b: a < c; c <= b"(c: this.width) if (dst_x & 7) == 0 { while args.src.length() <= 0, - inv dst_x < 0xFFFF_FFFF, - inv dst_y < 0xFFFF_FFFF, + inv dst_x < 0xFF_FFFF, + inv dst_y < 0xFF_FFFF, post args.src.length() > 0, { yield? base."$short read"