std: limit supported image dimensions to 0xFF_FFFF

This commit is contained in:
Nigel Tao committed 2023-04-16 08:57:55 +10:00
1 parent d8c23a3a66
commit e126c998e0
10 files changed
+175 -123

No files matched your search

+39 -25
View File
@@ -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
+12 -9
View File
@@ -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 ..]
+12 -8
View File
@@ -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
+6 -8
View File
@@ -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
+17 -20
View File
@@ -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"