Skip to content

Commit b84ce82

Browse files
committed
Clamp only derived sizes in ResizeImage instead of asserting in the constructor
An explicit width or height of 0 now fails at decode time, as before this change.
1 parent 6ff762f commit b84ce82

2 files changed

Lines changed: 34 additions & 27 deletions

File tree

‎packages/flutter/lib/src/painting/image_provider.dart‎

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,8 +1263,7 @@ class ResizeImage extends ImageProvider<ResizeImageKey> {
12631263
/// defined by `width` and `height`. The image will lose detail and
12641264
/// use less memory if resized to a size smaller than the native size.
12651265
///
1266-
/// At least one of `width` and `height` must be non-null. If they are
1267-
/// non-null, they must be greater than zero.
1266+
/// At least one of `width` and `height` must be non-null.
12681267
///
12691268
/// {@macro flutter.painting.ResizeImage.useLogicalSize}
12701269
const ResizeImage(
@@ -1274,9 +1273,7 @@ class ResizeImage extends ImageProvider<ResizeImageKey> {
12741273
this.policy = ResizeImagePolicy.exact,
12751274
this.allowUpscaling = false,
12761275
this.useLogicalSize = false,
1277-
}) : assert(width != null || height != null),
1278-
assert(width == null || width > 0),
1279-
assert(height == null || height > 0);
1276+
}) : assert(width != null || height != null);
12801277

12811278
/// The [ImageProvider] that this class wraps.
12821279
final ImageProvider imageProvider;
@@ -1415,6 +1412,8 @@ class ResizeImage extends ImageProvider<ResizeImageKey> {
14151412

14161413
return ui.TargetImageSize(width: targetWidth, height: targetHeight);
14171414
case ResizeImagePolicy.fit:
1415+
// A very thin image can derive a side of 0, which cannot be decoded.
1416+
// Clamp only the derived sides, so that a given side of 0 still fails.
14181417
final double aspectRatio = intrinsicWidth / intrinsicHeight;
14191418
final int maxWidth = effectiveWidth ?? intrinsicWidth;
14201419
final int maxHeight = effectiveHeight ?? intrinsicHeight;
@@ -1423,36 +1422,31 @@ class ResizeImage extends ImageProvider<ResizeImageKey> {
14231422

14241423
if (targetWidth > maxWidth) {
14251424
targetWidth = maxWidth;
1426-
targetHeight = targetWidth ~/ aspectRatio;
1425+
targetHeight = math.max(1, targetWidth ~/ aspectRatio);
14271426
}
14281427

14291428
if (targetHeight > maxHeight) {
14301429
targetHeight = maxHeight;
1431-
targetWidth = (targetHeight * aspectRatio).floor();
1430+
targetWidth = math.max(1, (targetHeight * aspectRatio).floor());
14321431
}
14331432

14341433
if (allowUpscaling) {
14351434
if (effectiveWidth == null) {
14361435
assert(effectiveHeight != null);
14371436
targetHeight = effectiveHeight!;
1438-
targetWidth = (targetHeight * aspectRatio).floor();
1437+
targetWidth = math.max(1, (targetHeight * aspectRatio).floor());
14391438
} else if (effectiveHeight == null) {
14401439
targetWidth = effectiveWidth;
1441-
targetHeight = targetWidth ~/ aspectRatio;
1440+
targetHeight = math.max(1, targetWidth ~/ aspectRatio);
14421441
} else {
1443-
final int derivedMaxWidth = (maxHeight * aspectRatio).floor();
1444-
final int derivedMaxHeight = maxWidth ~/ aspectRatio;
1442+
final int derivedMaxWidth = math.max(1, (maxHeight * aspectRatio).floor());
1443+
final int derivedMaxHeight = math.max(1, maxWidth ~/ aspectRatio);
14451444
targetWidth = math.min(maxWidth, derivedMaxWidth);
14461445
targetHeight = math.min(maxHeight, derivedMaxHeight);
14471446
}
14481447
}
14491448

1450-
// A very thin image can derive a side of 0, which cannot be decoded.
1451-
// The given sides are greater than 0, so this changes only derived sides.
1452-
return ui.TargetImageSize(
1453-
width: math.max(1, targetWidth),
1454-
height: math.max(1, targetHeight),
1455-
);
1449+
return ui.TargetImageSize(width: targetWidth, height: targetHeight);
14561450
}
14571451
},
14581452
);

‎packages/flutter/test/painting/image_provider_resize_image_test.dart‎

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ void main() {
2121
});
2222

2323
group('ResizeImage', () {
24-
test('asserts that width and height are greater than 0', () {
25-
final image = MemoryImage(Uint8List.fromList(kTransparentImage));
26-
for (final ResizeImagePolicy policy in ResizeImagePolicy.values) {
27-
for (final value in <int>[0, -1]) {
28-
expect(() => ResizeImage(image, width: value, policy: policy), throwsAssertionError);
29-
expect(() => ResizeImage(image, height: value, policy: policy), throwsAssertionError);
30-
expect(
31-
() => ResizeImage(image, width: 100, height: value, policy: policy),
32-
throwsAssertionError,
33-
);
24+
test('fails to decode when a given width or height is 0', () async {
25+
final image = MemoryImage(Uint8List.fromList(kBlueSquarePng));
26+
for (final policy in <ResizeImagePolicy>[ResizeImagePolicy.exact, ResizeImagePolicy.fit]) {
27+
// The constructor accepts 0. The failure occurs at decode time.
28+
for (final resizedImage in <ResizeImage>[
29+
ResizeImage(image, width: 0, policy: policy),
30+
ResizeImage(image, height: 0, policy: policy),
31+
]) {
32+
expect(await _resolveAndGetError(resizedImage), isAssertionError);
33+
PaintingBinding.instance.imageCache.clear();
3434
}
3535
}
3636
});
@@ -758,6 +758,19 @@ Future<Size> _resolveAndGetSize(
758758
return completer.future;
759759
}
760760

761+
Future<Object> _resolveAndGetError(ImageProvider imageProvider) {
762+
final completer = Completer<Object>();
763+
imageProvider
764+
.resolve(ImageConfiguration.empty)
765+
.addListener(
766+
ImageStreamListener(
767+
(ImageInfo image, bool synchronousCall) => completer.complete(image),
768+
onError: (Object error, StackTrace? stackTrace) => completer.complete(error),
769+
),
770+
);
771+
return completer.future;
772+
}
773+
761774
// This version of MemoryImage guarantees obtainKey returns a future that has not been
762775
// completed synchronously.
763776
class _AsyncKeyMemoryImage extends MemoryImage {

0 commit comments

Comments
 (0)