Skip to content

Commit 6ff762f

Browse files
committed
Assert that ResizeImage width and height are greater than 0
1 parent aeffa2a commit 6ff762f

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,7 +1263,8 @@ 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.
1266+
/// At least one of `width` and `height` must be non-null. If they are
1267+
/// non-null, they must be greater than zero.
12671268
///
12681269
/// {@macro flutter.painting.ResizeImage.useLogicalSize}
12691270
const ResizeImage(
@@ -1273,7 +1274,9 @@ class ResizeImage extends ImageProvider<ResizeImageKey> {
12731274
this.policy = ResizeImagePolicy.exact,
12741275
this.allowUpscaling = false,
12751276
this.useLogicalSize = false,
1276-
}) : assert(width != null || height != null);
1277+
}) : assert(width != null || height != null),
1278+
assert(width == null || width > 0),
1279+
assert(height == null || height > 0);
12771280

12781281
/// The [ImageProvider] that this class wraps.
12791282
final ImageProvider imageProvider;
@@ -1445,6 +1448,7 @@ class ResizeImage extends ImageProvider<ResizeImageKey> {
14451448
}
14461449

14471450
// 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.
14481452
return ui.TargetImageSize(
14491453
width: math.max(1, targetWidth),
14501454
height: math.max(1, targetHeight),

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ 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+
);
34+
}
35+
}
36+
});
37+
2438
group('equality and hashCode', () {
2539
test('Two identical instances should be equal and have same hashCode', () {
2640
final bytes = Uint8List.fromList(kTransparentImage);

0 commit comments

Comments
 (0)