Handle GIF frame out of bounds same as Chromium

This commit is contained in:
Nigel Tao committed 2018-05-23 15:59:41 +10:00
1 parent aa4029d29d
commit 66bcaf380d
4 files changed
+132 -126

No files matched your search

+45 -93
View File
@@ -1190,10 +1190,9 @@ typedef struct {
uint32_t f_width;
uint32_t f_height;
uint8_t f_call_sequence;
bool f_end_of_data;
bool f_previous_lzw_decode_ended_abruptly;
uint8_t f_background_color_index;
uint8_t f_block_type;
bool f_peek_block_type;
bool f_have_gct;
bool f_have_lct;
bool f_interlace;
@@ -1215,6 +1214,7 @@ typedef struct {
} c_decode_frame[1];
struct {
uint32_t coro_susp_point;
uint8_t v_block_type;
} c_decode_up_to_id_part1[1];
struct {
uint32_t coro_susp_point;
@@ -1978,22 +1978,6 @@ wuffs_gif__status wuffs_gif__decoder__decode_config(
}
wuffs_gif__status status = WUFFS_GIF__STATUS_OK;
uint8_t* ioptr_src = NULL;
uint8_t* iobounds0orig_src = NULL;
uint8_t* iobounds1_src = NULL;
WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(iobounds0orig_src);
WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(iobounds1_src);
if (a_src.private_impl.buf) {
ioptr_src = a_src.private_impl.buf->ptr + a_src.private_impl.buf->ri;
if (!a_src.private_impl.bounds[0]) {
a_src.private_impl.bounds[0] = ioptr_src;
a_src.private_impl.bounds[1] =
a_src.private_impl.buf->ptr + a_src.private_impl.buf->wi;
}
iobounds0orig_src = a_src.private_impl.bounds[0];
iobounds1_src = a_src.private_impl.bounds[1];
}
uint32_t coro_susp_point =
self->private_impl.c_decode_config[0].coro_susp_point;
if (coro_susp_point) {
@@ -2007,54 +1991,20 @@ wuffs_gif__status wuffs_gif__decoder__decode_config(
goto exit;
}
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
if (a_src.private_impl.buf) {
a_src.private_impl.buf->ri = ioptr_src - a_src.private_impl.buf->ptr;
}
status = wuffs_gif__decoder__decode_header(self, a_src);
if (a_src.private_impl.buf) {
ioptr_src = a_src.private_impl.buf->ptr + a_src.private_impl.buf->ri;
}
if (status) {
goto suspend;
}
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
if (a_src.private_impl.buf) {
a_src.private_impl.buf->ri = ioptr_src - a_src.private_impl.buf->ptr;
}
status = wuffs_gif__decoder__decode_lsd(self, a_src);
if (a_src.private_impl.buf) {
ioptr_src = a_src.private_impl.buf->ptr + a_src.private_impl.buf->ri;
}
if (status) {
goto suspend;
}
while (true) {
self->private_impl.f_peek_block_type = true;
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
if (WUFFS_BASE__UNLIKELY(ioptr_src == iobounds1_src)) {
goto short_read_src;
}
uint8_t t_0 = *ioptr_src++;
self->private_impl.f_block_type = t_0;
}
if (self->private_impl.f_seen_num_loops ||
(self->private_impl.f_block_type != 33)) {
goto label_0_break;
}
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4);
if (a_src.private_impl.buf) {
a_src.private_impl.buf->ri = ioptr_src - a_src.private_impl.buf->ptr;
}
status = wuffs_gif__decoder__decode_extension(self, a_src);
if (a_src.private_impl.buf) {
ioptr_src = a_src.private_impl.buf->ptr + a_src.private_impl.buf->ri;
}
if (status) {
goto suspend;
}
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
status = wuffs_gif__decoder__decode_up_to_id_part1(self, a_src);
if (status) {
goto suspend;
}
label_0_break:;
wuffs_base__image_config__initialize(
a_dst, 570984584, 0, self->private_impl.f_width,
self->private_impl.f_height, self->private_impl.f_num_loops);
@@ -2072,20 +2022,8 @@ suspend:
goto exit;
exit:
if (a_src.private_impl.buf) {
a_src.private_impl.buf->ri = ioptr_src - a_src.private_impl.buf->ptr;
}
self->private_impl.status = status;
return status;
short_read_src:
if (wuffs_base__io_reader__is_eof(a_src)) {
status = WUFFS_GIF__ERROR_UNEXPECTED_EOF;
goto exit;
}
status = WUFFS_GIF__SUSPENSION_SHORT_READ;
goto suspend;
}
// -------- func decoder.decode_frame
@@ -2118,16 +2056,28 @@ wuffs_gif__status wuffs_gif__decoder__decode_frame(
switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
if (self->private_impl.f_call_sequence < 1) {
if (self->private_impl.f_call_sequence == 0) {
status = WUFFS_GIF__ERROR_INVALID_CALL_SEQUENCE;
goto exit;
}
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
status = wuffs_gif__decoder__decode_up_to_id_part1(self, a_src);
if (status) {
goto suspend;
if (!self->private_impl.f_end_of_data) {
if (self->private_impl.f_call_sequence == 1) {
self->private_impl.f_call_sequence = 2;
} else {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
status = wuffs_gif__decoder__decode_up_to_id_part1(self, a_src);
if (status) {
goto suspend;
}
}
}
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
if (self->private_impl.f_end_of_data) {
while (true) {
status = WUFFS_GIF__SUSPENSION_END_OF_DATA;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(2);
}
}
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
status = wuffs_gif__decoder__decode_id_part1(self, a_ib, a_dst, a_src);
if (status) {
goto suspend;
@@ -2165,6 +2115,8 @@ wuffs_gif__status wuffs_gif__decoder__decode_up_to_id_part1(
}
wuffs_gif__status status = WUFFS_GIF__STATUS_OK;
uint8_t v_block_type;
uint8_t* ioptr_src = NULL;
uint8_t* iobounds0orig_src = NULL;
uint8_t* iobounds1_src = NULL;
@@ -2184,25 +2136,22 @@ wuffs_gif__status wuffs_gif__decoder__decode_up_to_id_part1(
uint32_t coro_susp_point =
self->private_impl.c_decode_up_to_id_part1[0].coro_susp_point;
if (coro_susp_point) {
v_block_type = self->private_impl.c_decode_up_to_id_part1[0].v_block_type;
} else {
}
switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
while (true) {
if (self->private_impl.f_peek_block_type) {
self->private_impl.f_peek_block_type = false;
} else {
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
if (WUFFS_BASE__UNLIKELY(ioptr_src == iobounds1_src)) {
goto short_read_src;
}
uint8_t t_0 = *ioptr_src++;
self->private_impl.f_block_type = t_0;
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
if (WUFFS_BASE__UNLIKELY(ioptr_src == iobounds1_src)) {
goto short_read_src;
}
uint8_t t_0 = *ioptr_src++;
v_block_type = t_0;
}
if (self->private_impl.f_block_type == 33) {
if (v_block_type == 33) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
if (a_src.private_impl.buf) {
a_src.private_impl.buf->ri = ioptr_src - a_src.private_impl.buf->ptr;
@@ -2214,7 +2163,7 @@ wuffs_gif__status wuffs_gif__decoder__decode_up_to_id_part1(
if (status) {
goto suspend;
}
} else if (self->private_impl.f_block_type == 44) {
} else if (v_block_type == 44) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
if (a_src.private_impl.buf) {
a_src.private_impl.buf->ri = ioptr_src - a_src.private_impl.buf->ptr;
@@ -2226,9 +2175,9 @@ wuffs_gif__status wuffs_gif__decoder__decode_up_to_id_part1(
if (status) {
goto suspend;
}
status = WUFFS_GIF__STATUS_OK;
goto ok;
} else if (self->private_impl.f_block_type == 59) {
goto label_0_break;
} else if (v_block_type == 59) {
self->private_impl.f_end_of_data = true;
goto label_0_break;
} else {
status = WUFFS_GIF__ERROR_BAD_BLOCK;
@@ -2236,10 +2185,6 @@ wuffs_gif__status wuffs_gif__decoder__decode_up_to_id_part1(
}
}
label_0_break:;
while (true) {
status = WUFFS_GIF__SUSPENSION_END_OF_DATA;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(4);
}
goto ok;
ok:
@@ -2251,6 +2196,7 @@ wuffs_gif__status wuffs_gif__decoder__decode_up_to_id_part1(
suspend:
self->private_impl.c_decode_up_to_id_part1[0].coro_susp_point =
coro_susp_point;
self->private_impl.c_decode_up_to_id_part1[0].v_block_type = v_block_type;
goto exit;
exit:
@@ -3032,6 +2978,12 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id_part0(
&self->private_impl.f_frame_rect, v_frame_x);
wuffs_base__rect_ie_u32__set_max_exclusive_y(
&self->private_impl.f_frame_rect, v_frame_y);
if (self->private_impl.f_call_sequence == 0) {
self->private_impl.f_width =
wuffs_base__u32__max(self->private_impl.f_width, v_frame_x);
self->private_impl.f_height =
wuffs_base__u32__max(self->private_impl.f_height, v_frame_y);
}
goto ok;
ok:
+2 -2
View File
@@ -1190,10 +1190,9 @@ typedef struct {
uint32_t f_width;
uint32_t f_height;
uint8_t f_call_sequence;
bool f_end_of_data;
bool f_previous_lzw_decode_ended_abruptly;
uint8_t f_background_color_index;
uint8_t f_block_type;
bool f_peek_block_type;
bool f_have_gct;
bool f_have_lct;
bool f_interlace;
@@ -1215,6 +1214,7 @@ typedef struct {
} c_decode_frame[1];
struct {
uint32_t coro_susp_point;
uint8_t v_block_type;
} c_decode_up_to_id_part1[1];
struct {
uint32_t coro_susp_point;
+49 -31
View File
@@ -20,21 +20,26 @@ pub error "bad header"
pub error "bad literal width"
pub struct decoder?(
width base.u32[..65535],
height base.u32[..65535],
width base.u32,
height base.u32,
// Call sequence states:
// - 0: initial state.
// - 1: image config decoded, including the first frame's bounds, but not
// the first frame's pixels.
// - 2: frame decoded.
//
// Call sequence state transitions:
// - 0 -> 1: via decode_config.
// - 1 -> 1: via decode_frame.
// - 1 -> 2: via decode_frame.
// - 2 -> 2: via decode_frame.
call_sequence base.u8,
end_of_data base.bool,
previous_lzw_decode_ended_abruptly base.bool,
background_color_index base.u8,
block_type base.u8,
peek_block_type base.bool,
have_gct base.bool,
have_lct base.bool,
@@ -67,15 +72,9 @@ pub func decoder.decode_config?(dst ptr base.image_config, src base.io_reader)()
}
this.decode_header?(src:in.src)
this.decode_lsd?(src:in.src)
this.decode_up_to_id_part1?(src:in.src)
while true {
this.peek_block_type = true
this.block_type = in.src.read_u8?()
if this.seen_num_loops or (this.block_type != 0x21) { // The spec calls 0x21 the "Extension Introducer".
break
}
this.decode_extension?(src:in.src)
}
// TODO: if this.end_of_data, return an error and/or set dst to zero?
// TODO: rename initialize to set?
//
@@ -83,44 +82,54 @@ pub func decoder.decode_config?(dst ptr base.image_config, src base.io_reader)()
// WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL_INDEXED magic pixfmt constant.
in.dst.initialize!(pixfmt:0x22088888, pixsub:0,
width:this.width, height:this.height, num_loops:this.num_loops)
this.call_sequence = 1
}
pub func decoder.decode_frame?(ib ptr base.image_buffer, dst base.io_writer, src base.io_reader)() {
if this.call_sequence < 1 {
if this.call_sequence == 0 {
return error "invalid call sequence"
}
this.decode_up_to_id_part1?(src:in.src)
if not this.end_of_data {
if this.call_sequence == 1 {
// TODO: memset dst's pixel_buffer (and palette) to zero?
this.call_sequence = 2
} else {
this.decode_up_to_id_part1?(src:in.src)
}
}
// This is a new "if", not an "else", because calling
// this.decode_up_to_id_part1 can modify this.end_of_data.
if this.end_of_data {
while true {
yield suspension "end of data"
}
}
this.decode_id_part1?(ib:in.ib, dst:in.dst, src:in.src)
}
pub func decoder.decode_up_to_id_part1?(src base.io_reader)() {
while true {
if this.peek_block_type {
this.peek_block_type = false
} else {
this.block_type = in.src.read_u8?()
}
if this.block_type == 0x21 { // The spec calls 0x21 the "Extension Introducer".
var block_type base.u8 = in.src.read_u8?()
if block_type == 0x21 { // The spec calls 0x21 the "Extension Introducer".
this.decode_extension?(src:in.src)
} else if this.block_type == 0x2C { // The spec calls 0x2C the "Image Separator".
} else if block_type == 0x2C { // The spec calls 0x2C the "Image Separator".
// TODO: animated GIFs can have multiple Image Descriptors. What's
// the API? Should the 0x3B Trailer byte result in a "no more
// frames" suspension instead of ok? Do we need additional
// destination arguments for timing and palette?
this.decode_id_part0?(src:in.src)
return
} else if this.block_type == 0x3B { // The spec calls 0x3B the "Trailer".
break
} else if block_type == 0x3B { // The spec calls 0x3B the "Trailer".
this.end_of_data = true
break
} else {
return error "bad block"
}
}
while true {
yield suspension "end of data"
}
}
// decode_header reads either "GIF87a" or "GIF89a".
@@ -285,7 +294,6 @@ pri func decoder.decode_ae?(src base.io_reader)() {
// width and height also requires decoding the first frame's bounds (but
// doesn't require decoding the first frame's pixels).
pri func decoder.decode_id_part0?(src base.io_reader)() {
// TODO: check that the frame rect is inside the image rect??
var frame_x base.u32 = in.src.read_u16le?() as base.u32
var frame_y base.u32 = in.src.read_u16le?() as base.u32
this.frame_rect.set_min_inclusive_x!(x:frame_x)
@@ -294,6 +302,16 @@ pri func decoder.decode_id_part0?(src base.io_reader)() {
frame_y ~mod+= in.src.read_u16le?() as base.u32
this.frame_rect.set_max_exclusive_x!(x:frame_x)
this.frame_rect.set_max_exclusive_y!(y:frame_y)
// Set the image's overall width and height to be the maximum of the
// nominal image width and height (given in the Logical Screen Descriptor)
// and the bottom right extent of the first frame. See
// test/data/artificial/gif-frame-out-of-bounds.gif.make-artificial.txt for
// more discussion.
if this.call_sequence == 0 {
this.width = this.width.max(x:frame_x)
this.height = this.height.max(x:frame_y)
}
}
// TODO: dst should be a ptr base.image_buffer, not a base.io_writer.
+36
View File
@@ -658,6 +658,41 @@ void test_wuffs_gif_decode_animated_small() {
3, 4);
}
void test_wuffs_gif_decode_frame_out_of_bounds() {
CHECK_FOCUS(__func__);
wuffs_base__io_buffer src = {.ptr = global_src_buffer, .len = BUFFER_SIZE};
if (!read_file(&src, "../../data/artificial/gif-frame-out-of-bounds.gif")) {
return;
}
wuffs_gif__decoder dec;
wuffs_gif__decoder__initialize(&dec, WUFFS_VERSION, 0);
wuffs_base__image_config ic = {{0}};
wuffs_base__io_reader src_reader = wuffs_base__io_buffer__reader(&src);
wuffs_gif__status s =
wuffs_gif__decoder__decode_config(&dec, &ic, src_reader);
if (s) {
FAIL("decode_config: %s", wuffs_gif__status__string(s));
return;
}
// The nominal width and height for the overall image is 2×2, but its first
// frame extends those bounds to 4×2. See
// test/data/artificial/gif-frame-out-of-bounds.gif.make-artificial.txt for
// more discussion.
if (wuffs_base__image_config__width(&ic) != 4) {
FAIL("width: got %" PRIu32 ", want 4",
wuffs_base__image_config__width(&ic));
return;
}
if (wuffs_base__image_config__height(&ic) != 2) {
FAIL("height: got %" PRIu32 ", want 2",
wuffs_base__image_config__height(&ic));
return;
}
}
void test_wuffs_gif_decode_input_is_a_gif() {
CHECK_FOCUS(__func__);
do_test_wuffs_gif_decode("../../data/bricks-dither.gif",
@@ -899,6 +934,7 @@ proc tests[] = {
test_wuffs_gif_decode_animated_big, //
test_wuffs_gif_decode_animated_medium, //
test_wuffs_gif_decode_animated_small, //
test_wuffs_gif_decode_frame_out_of_bounds, //
test_wuffs_gif_decode_input_is_a_gif, //
test_wuffs_gif_decode_input_is_a_gif_many_big_reads, //
test_wuffs_gif_decode_input_is_a_gif_many_medium_reads, //