From d7f434c6e454531c4a029c54f2067701270fe77b Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sat, 23 Feb 2019 14:29:16 +1100 Subject: [PATCH] Have the GIF decoder check for "not enough data" Fixes #20 --- fuzz/c/std/gif_fuzzer.c | 18 ++- lang/builtin/builtin.go | 1 + release/c/wuffs-unsupported-snapshot.c | 10 ++ std/gif/decode_gif.wuffs | 6 +- test/c/std/gif.c | 124 +++++++++++++----- test/data/artificial/gif-not-enough-data.gif | Bin 0 -> 40 bytes ...if-not-enough-data.gif.make-artificial.txt | 23 ++++ test/data/artificial/gif-too-much-data.gif | Bin 0 -> 41 bytes .../gif-too-much-data.gif.make-artificial.txt | 23 ++++ 9 files changed, 162 insertions(+), 43 deletions(-) create mode 100644 test/data/artificial/gif-not-enough-data.gif create mode 100644 test/data/artificial/gif-not-enough-data.gif.make-artificial.txt create mode 100644 test/data/artificial/gif-too-much-data.gif create mode 100644 test/data/artificial/gif-too-much-data.gif.make-artificial.txt diff --git a/fuzz/c/std/gif_fuzzer.c b/fuzz/c/std/gif_fuzzer.c index 68ccf7ca..c3ce2fd9 100644 --- a/fuzz/c/std/gif_fuzzer.c +++ b/fuzz/c/std/gif_fuzzer.c @@ -125,6 +125,16 @@ const char* fuzz(wuffs_base__io_reader src_reader, uint32_t hash) { status = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader, workbuf, NULL); + + wuffs_base__rect_ie_u32 frame_rect = + wuffs_base__frame_config__bounds(&fc); + wuffs_base__rect_ie_u32 dirty_rect = + wuffs_gif__decoder__frame_dirty_rect(&dec); + if (!wuffs_base__rect_ie_u32__contains_rect(&frame_rect, dirty_rect)) { + ret = "internal error: frame_rect does not contain dirty_rect"; + goto exit; + } + if (status) { if ((status != wuffs_base__warning__end_of_data) || !seen_ok) { ret = status; @@ -133,12 +143,8 @@ const char* fuzz(wuffs_base__io_reader src_reader, uint32_t hash) { } seen_ok = true; - wuffs_base__rect_ie_u32 frame_rect = - wuffs_base__frame_config__bounds(&fc); - wuffs_base__rect_ie_u32 dirty_rect = - wuffs_gif__decoder__frame_dirty_rect(&dec); - if (!wuffs_base__rect_ie_u32__contains_rect(&frame_rect, dirty_rect)) { - ret = "internal error: frame_rect does not contain dirty_rect"; + if (!wuffs_base__rect_ie_u32__equals(&frame_rect, dirty_rect)) { + ret = "internal error: frame_rect does not equal dirty_rect"; goto exit; } } diff --git a/lang/builtin/builtin.go b/lang/builtin/builtin.go index 492a92a3..ea398682 100644 --- a/lang/builtin/builtin.go +++ b/lang/builtin/builtin.go @@ -41,6 +41,7 @@ var Statuses = [...]string{ `"?initialize falsely claimed already zeroed"`, `"?initialize not called"`, `"?interleaved coroutine calls"`, + `"?not enough data"`, } // TODO: a collection of forbidden variable names like and, or, not, as, ref, diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index ea792585..c6c0f66c 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -185,6 +185,7 @@ extern const char* wuffs_base__error__disabled_by_previous_error; extern const char* wuffs_base__error__initialize_falsely_claimed_already_zeroed; extern const char* wuffs_base__error__initialize_not_called; extern const char* wuffs_base__error__interleaved_coroutine_calls; +extern const char* wuffs_base__error__not_enough_data; static inline bool // wuffs_base__status__is_complete(wuffs_base__status z) { @@ -4806,6 +4807,7 @@ const char* wuffs_base__error__initialize_not_called = "?base: initialize not called"; const char* wuffs_base__error__interleaved_coroutine_calls = "?base: interleaved coroutine calls"; +const char* wuffs_base__error__not_enough_data = "?base: not enough data"; // ---------------- Images @@ -10686,6 +10688,14 @@ wuffs_gif__decoder__decode_id_part2(wuffs_gif__decoder* self, label_0_break:; self->private_impl.f_compressed_ri = 0; self->private_impl.f_compressed_wi = 0; + if ((self->private_impl.f_dst_y < self->private_impl.f_frame_rect_y1) && + (self->private_impl.f_frame_rect_x0 != + self->private_impl.f_frame_rect_x1) && + (self->private_impl.f_frame_rect_y0 != + self->private_impl.f_frame_rect_y1)) { + status = wuffs_base__error__not_enough_data; + goto exit; + } goto ok; ok: diff --git a/std/gif/decode_gif.wuffs b/std/gif/decode_gif.wuffs index b9dc0475..dbeb1e6d 100644 --- a/std/gif/decode_gif.wuffs +++ b/std/gif/decode_gif.wuffs @@ -763,7 +763,11 @@ pri func decoder.decode_id_part2?(dst ptr base.pixel_buffer, src base.io_reader, this.compressed_ri = 0 this.compressed_wi = 0 - // TODO: check for "not enough pixel data". + if (this.dst_y < this.frame_rect_y1) and + (this.frame_rect_x0 != this.frame_rect_x1) and + (this.frame_rect_y0 != this.frame_rect_y1) { + return base."?not enough data" + } } pri func decoder.copy_to_image_buffer!(pb ptr base.pixel_buffer, src slice base.u8) base.status { diff --git a/test/c/std/gif.c b/test/c/std/gif.c index b3f941df..5e04f2fd 100644 --- a/test/c/std/gif.c +++ b/test/c/std/gif.c @@ -467,6 +467,48 @@ const char* do_test_wuffs_gif_decode(const char* filename, return NULL; } +const char* do_test_wuffs_gif_decode_expecting(wuffs_base__io_buffer src, + const char* want_status, + bool want_dirty_rect_is_empty) { + wuffs_gif__decoder dec; + const char* status = wuffs_gif__decoder__initialize( + &dec, sizeof dec, WUFFS_VERSION, + WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED); + if (status) { + RETURN_FAIL("initialize: \"%s\"", status); + } + + wuffs_base__image_config ic = ((wuffs_base__image_config){}); + wuffs_base__io_reader src_reader = wuffs_base__io_buffer__reader(&src); + + status = wuffs_gif__decoder__decode_image_config(&dec, &ic, src_reader); + if (status) { + RETURN_FAIL("decode_image_config: got \"%s\"", status); + } + + wuffs_base__pixel_buffer pb = ((wuffs_base__pixel_buffer){}); + status = wuffs_base__pixel_buffer__set_from_slice(&pb, &ic.pixcfg, + global_pixel_slice); + if (status) { + RETURN_FAIL("set_from_slice: \"%s\"", status); + } + + status = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader, + global_work_slice, NULL); + if (status != want_status) { + RETURN_FAIL("decode_frame: got \"%s\", want \"%s\"", status, want_status); + } + + wuffs_base__rect_ie_u32 r = wuffs_gif__decoder__frame_dirty_rect(&dec); + bool dirty_rect_is_empty = wuffs_base__rect_ie_u32__is_empty(&r); + if (dirty_rect_is_empty != want_dirty_rect_is_empty) { + RETURN_FAIL("dirty_rect.is_empty: got %s, want %s", + dirty_rect_is_empty ? "true" : "false", + want_dirty_rect_is_empty ? "true" : "false"); + } + return NULL; +} + const char* test_wuffs_gif_call_interleaved() { CHECK_FOCUS(__func__); @@ -906,42 +948,8 @@ const char* test_wuffs_gif_decode_missing_two_src_bytes() { } src.meta.wi -= 2; - wuffs_gif__decoder dec; - status = wuffs_gif__decoder__initialize( - &dec, sizeof dec, WUFFS_VERSION, - WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED); - if (status) { - RETURN_FAIL("initialize: \"%s\"", status); - } - - wuffs_base__image_config ic = ((wuffs_base__image_config){}); - wuffs_base__io_reader src_reader = wuffs_base__io_buffer__reader(&src); - - status = wuffs_gif__decoder__decode_image_config(&dec, &ic, src_reader); - if (status) { - RETURN_FAIL("decode_image_config: got \"%s\"", status); - } - - wuffs_base__pixel_buffer pb = ((wuffs_base__pixel_buffer){}); - status = wuffs_base__pixel_buffer__set_from_slice(&pb, &ic.pixcfg, - global_pixel_slice); - if (status) { - RETURN_FAIL("set_from_slice: \"%s\"", status); - } - - status = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader, - global_work_slice, NULL); - if (status != wuffs_base__suspension__short_read) { - RETURN_FAIL("decode_frame: got \"%s\"", status); - } - - // Even we though we haven't seen the 0x00 end-of-block, we should still have - // some pixel data. - wuffs_base__rect_ie_u32 r = wuffs_gif__decoder__frame_dirty_rect(&dec); - if (wuffs_base__rect_ie_u32__is_empty(&r)) { - RETURN_FAIL("dirty_rect: got empty, want non-empty"); - } - return NULL; + return do_test_wuffs_gif_decode_expecting( + src, wuffs_base__suspension__short_read, false); } const char* test_wuffs_gif_decode_multiple_loop_counts() { @@ -1011,6 +1019,48 @@ const char* test_wuffs_gif_decode_multiple_loop_counts() { return NULL; } +const char* test_wuffs_gif_decode_not_enough_data() { + CHECK_FOCUS(__func__); + + wuffs_base__io_buffer src = ((wuffs_base__io_buffer){ + .data = global_src_slice, + }); + + const char* status = + read_file(&src, "test/data/artificial/gif-not-enough-data.gif"); + if (status) { + return status; + } + + // TODO: want_dirty_rect_is_empty should probably be false, not true. It's + // true because the error sets the decoder state (specifically, the + // private_impl.magic field) to WUFFS_BASE__DISABLED, so that + // frame_dirty_rect returns the zero value (an empty rect). + // + // Perhaps "not enough data" should be a warning, not an error?? + // + // Ditto for the "too much data" test. + return do_test_wuffs_gif_decode_expecting( + src, wuffs_base__error__not_enough_data, true); +} + +const char* test_wuffs_gif_decode_too_much_data() { + CHECK_FOCUS(__func__); + + wuffs_base__io_buffer src = ((wuffs_base__io_buffer){ + .data = global_src_slice, + }); + + const char* status = + read_file(&src, "test/data/artificial/gif-too-much-data.gif"); + if (status) { + return status; + } + + return do_test_wuffs_gif_decode_expecting( + src, wuffs_gif__error__too_much_pixel_data, true); +} + const char* test_wuffs_gif_frame_dirty_rect() { CHECK_FOCUS(__func__); @@ -1627,6 +1677,8 @@ proc tests[] = { test_wuffs_gif_decode_input_is_a_png, // test_wuffs_gif_decode_missing_two_src_bytes, // test_wuffs_gif_decode_multiple_loop_counts, // + test_wuffs_gif_decode_not_enough_data, // + test_wuffs_gif_decode_too_much_data, // test_wuffs_gif_frame_dirty_rect, // test_wuffs_gif_num_decoded_frame_configs, // test_wuffs_gif_num_decoded_frames, // diff --git a/test/data/artificial/gif-not-enough-data.gif b/test/data/artificial/gif-not-enough-data.gif new file mode 100644 index 0000000000000000000000000000000000000000..016857fac8a2e513114301f3ceedcd76ae130c5f GIT binary patch literal 40 rcmZ?wbhEHbWMW`qXk=huVE8Y{@L!4HzcIsq9S|2J&cMW|!C(yls^bSH literal 0 HcmV?d00001 diff --git a/test/data/artificial/gif-not-enough-data.gif.make-artificial.txt b/test/data/artificial/gif-not-enough-data.gif.make-artificial.txt new file mode 100644 index 00000000..adc7ddea --- /dev/null +++ b/test/data/artificial/gif-not-enough-data.gif.make-artificial.txt @@ -0,0 +1,23 @@ +# Feed this file to script/make-artificial.go + +make gif + +header + +image { + imageWidthHeight 2 2 + palette { + 0x00 0x00 0xFF + 0x11 0x00 0xFF + 0x22 0x00 0xFF + 0x33 0x00 0xFF + } +} + +frame { + frameLeftTopWidthHeight 0 0 2 2 +} +# The frame is 4 pixels, but we only supply 1. +lzw 2 0x00 + +trailer diff --git a/test/data/artificial/gif-too-much-data.gif b/test/data/artificial/gif-too-much-data.gif new file mode 100644 index 0000000000000000000000000000000000000000..573ec88808e99027922e987fb0986658514086c8 GIT binary patch literal 41 scmZ?wbhEHbWMW`qXk=huVE8Y{@L!4HzcIsq9S|2J&cMX9ft$e^0JIbbx&QzG literal 0 HcmV?d00001 diff --git a/test/data/artificial/gif-too-much-data.gif.make-artificial.txt b/test/data/artificial/gif-too-much-data.gif.make-artificial.txt new file mode 100644 index 00000000..5f02c1b6 --- /dev/null +++ b/test/data/artificial/gif-too-much-data.gif.make-artificial.txt @@ -0,0 +1,23 @@ +# Feed this file to script/make-artificial.go + +make gif + +header + +image { + imageWidthHeight 2 2 + palette { + 0x00 0x00 0xFF + 0x11 0x00 0xFF + 0x22 0x00 0xFF + 0x33 0x00 0xFF + } +} + +frame { + frameLeftTopWidthHeight 0 0 2 2 +} +# The frame is 4 pixels, but we supply 5. +lzw 2 0x00 0x00 0x00 0x00 0x00 + +trailer