Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit f8c8f27

Browse files
Propagate image decode errors to the future returned by Codec.getNextFrame (#24336)
1 parent 0fa87b4 commit f8c8f27

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

lib/ui/painting.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1939,12 +1939,13 @@ class Codec extends NativeFieldWrapperClass2 {
19391939
final Completer<FrameInfo> completer = Completer<FrameInfo>.sync();
19401940
final String? error = _getNextFrame((_Image? image, int durationMilliseconds) {
19411941
if (image == null) {
1942-
throw Exception('Codec failed to produce an image, possibly due to invalid image data.');
1942+
completer.completeError(Exception('Codec failed to produce an image, possibly due to invalid image data.'));
1943+
} else {
1944+
completer.complete(FrameInfo._(
1945+
image: Image._(image),
1946+
duration: Duration(milliseconds: durationMilliseconds),
1947+
));
19431948
}
1944-
completer.complete(FrameInfo._(
1945-
image: Image._(image),
1946-
duration: Duration(milliseconds: durationMilliseconds),
1947-
));
19481949
});
19491950
if (error != null) {
19501951
throw Exception(error);

testing/dart/codec_test.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ void main() {
3434
);
3535
});
3636

37+
test('getNextFrame fails with invalid data', () async {
38+
Uint8List data = await _getSkiaResource('flutter_logo.jpg').readAsBytes();
39+
data = Uint8List.view(data.buffer, 0, 4000);
40+
final ui.Codec codec = await ui.instantiateImageCodec(data);
41+
try {
42+
await codec.getNextFrame();
43+
fail('exception not thrown');
44+
} catch(e) {
45+
expect(e, exceptionWithMessage('Codec failed'));
46+
}
47+
});
48+
3749
test('nextFrame', () async {
3850
final Uint8List data = await _getSkiaResource('test640x479.gif').readAsBytes();
3951
final ui.Codec codec = await ui.instantiateImageCodec(data);

0 commit comments

Comments
 (0)