diff --git a/engine/src/flutter/lib/ui/fixtures/out_of_bounds_wrapping.apng b/engine/src/flutter/lib/ui/fixtures/out_of_bounds_wrapping.apng new file mode 100644 index 0000000000..e4cc5d50cf Binary files /dev/null and b/engine/src/flutter/lib/ui/fixtures/out_of_bounds_wrapping.apng differ diff --git a/engine/src/flutter/lib/ui/painting/image_generator_apng.cc b/engine/src/flutter/lib/ui/painting/image_generator_apng.cc index 77b7b23767..08182c9ff2 100644 --- a/engine/src/flutter/lib/ui/painting/image_generator_apng.cc +++ b/engine/src/flutter/lib/ui/painting/image_generator_apng.cc @@ -110,7 +110,15 @@ bool APNGImageGenerator::GetPixels(const SkImageInfo& info, << ") of APNG due to the frame missing data (frame_info)."; return false; } - if (frame.x_offset + frame_info.width() > + if ( + // Check for unsigned integer wrapping for + // frame.{x|y}_offset + frame_info.{width|height}(). + frame.x_offset > + std::numeric_limits::max() - frame_info.width() || + frame.y_offset > + std::numeric_limits::max() - frame_info.height() || + + frame.x_offset + frame_info.width() > static_cast(info.width()) || frame.y_offset + frame_info.height() > static_cast(info.height())) { diff --git a/engine/src/flutter/testing/dart/codec_test.dart b/engine/src/flutter/testing/dart/codec_test.dart index 197369c609..7d0a2a23d2 100644 --- a/engine/src/flutter/testing/dart/codec_test.dart +++ b/engine/src/flutter/testing/dart/codec_test.dart @@ -272,6 +272,26 @@ void main() { } } }); + + test( + 'Animated apng frame decode does not crash with invalid destination region and bounds wrapping', + () async { + final Uint8List data = File( + path.join('flutter', 'lib', 'ui', 'fixtures', 'out_of_bounds_wrapping.apng'), + ).readAsBytesSync(); + + final ui.Codec codec = await ui.instantiateImageCodec(data); + try { + await codec.getNextFrame(); + fail('exception not thrown'); + } on Exception catch (e) { + if (impellerEnabled) { + expect(e.toString(), contains('Could not decompress image.')); + } else { + expect(e.toString(), contains('Codec failed')); + } + } + }); } /// Returns a File handle to a file in the skia/resources directory.