Visitar URL original
Bound resize dimensions to coordinate limit · lovell/sharp@8044fe4 · GitHub
Skip to content

Commit 8044fe4

Browse files
committed
Bound resize dimensions to coordinate limit
1 parent 147f859 commit 8044fe4

3 files changed

Lines changed: 44 additions & 14 deletions

File tree

‎docs/src/content/docs/changelog/v0.35.4.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ title: v0.35.4 - TBD
33
slug: changelog/v0.35.4
44
---
55

6+
* Bound resize dimensions to coordinate limit.
7+
68
* Bound composite left and top to coordinate limit.
79
[#4564](https://github.com/lovell/sharp/pull/4564)
810
[@metsw24-max](https://github.com/metsw24-max)

‎lib/resize.mjs‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -265,38 +265,38 @@ function resize (widthOrOptions, height, options) {
265265
if (is.defined(widthOrOptions)) {
266266
if (is.object(widthOrOptions) && !is.defined(options)) {
267267
options = widthOrOptions;
268-
} else if (is.integer(widthOrOptions) && widthOrOptions > 0) {
268+
} else if (is.integer(widthOrOptions) && is.inRange(widthOrOptions, 1, 100000000)) {
269269
this.options.width = widthOrOptions;
270270
} else {
271-
throw is.invalidParameterError('width', 'positive integer', widthOrOptions);
271+
throw is.invalidParameterError('width', 'positive integer between 1 and 100000000', widthOrOptions);
272272
}
273273
} else {
274274
this.options.width = -1;
275275
}
276276
if (is.defined(height)) {
277-
if (is.integer(height) && height > 0) {
277+
if (is.integer(height) && is.inRange(height, 1, 100000000)) {
278278
this.options.height = height;
279279
} else {
280-
throw is.invalidParameterError('height', 'positive integer', height);
280+
throw is.invalidParameterError('height', 'positive integer between 1 and 100000000', height);
281281
}
282282
} else {
283283
this.options.height = -1;
284284
}
285285
if (is.object(options)) {
286286
// Width
287287
if (is.defined(options.width)) {
288-
if (is.integer(options.width) && options.width > 0) {
288+
if (is.integer(options.width) && is.inRange(options.width, 1, 100000000)) {
289289
this.options.width = options.width;
290290
} else {
291-
throw is.invalidParameterError('width', 'positive integer', options.width);
291+
throw is.invalidParameterError('width', 'positive integer between 1 and 100000000', options.width);
292292
}
293293
}
294294
// Height
295295
if (is.defined(options.height)) {
296-
if (is.integer(options.height) && options.height > 0) {
296+
if (is.integer(options.height) && is.inRange(options.height, 1, 100000000)) {
297297
this.options.height = options.height;
298298
} else {
299-
throw is.invalidParameterError('height', 'positive integer', options.height);
299+
throw is.invalidParameterError('height', 'positive integer between 1 and 100000000', options.height);
300300
}
301301
}
302302
// Fit

‎test/unit/resize.js‎

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,42 +58,70 @@ suite('Resize dimensions', () => {
5858
t.plan(1);
5959
t.assert.throws(() => {
6060
sharp().resize('spoons', 240);
61-
}, /Expected positive integer for width but received spoons of type string/);
61+
}, /Expected positive integer between 1 and 100000000 for width but received spoons of type string/);
6262
});
6363

6464
test('Invalid height - NaN', (t) => {
6565
t.plan(1);
6666
t.assert.throws(() => {
6767
sharp().resize(320, 'spoons');
68-
}, /Expected positive integer for height but received spoons of type string/);
68+
}, /Expected positive integer between 1 and 100000000 for height but received spoons of type string/);
6969
});
7070

7171
test('Invalid width - float', (t) => {
7272
t.plan(1);
7373
t.assert.throws(() => {
7474
sharp().resize(1.5, 240);
75-
}, /Expected positive integer for width but received 1.5 of type number/);
75+
}, /Expected positive integer between 1 and 100000000 for width but received 1.5 of type number/);
7676
});
7777

7878
test('Invalid height - float', (t) => {
7979
t.plan(1);
8080
t.assert.throws(() => {
8181
sharp().resize(320, 1.5);
82-
}, /Expected positive integer for height but received 1.5 of type number/);
82+
}, /Expected positive integer between 1 and 100000000 for height but received 1.5 of type number/);
8383
});
8484

8585
test('Invalid width - via options', (t) => {
8686
t.plan(1);
8787
t.assert.throws(() => {
8888
sharp().resize({ width: 1.5, height: 240 });
89-
}, /Expected positive integer for width but received 1.5 of type number/);
89+
}, /Expected positive integer between 1 and 100000000 for width but received 1.5 of type number/);
9090
});
9191

9292
test('Invalid height - via options', (t) => {
9393
t.plan(1);
9494
t.assert.throws(() => {
9595
sharp().resize({ width: 320, height: 1.5 });
96-
}, /Expected positive integer for height but received 1.5 of type number/);
96+
}, /Expected positive integer between 1 and 100000000 for height but received 1.5 of type number/);
97+
});
98+
99+
test('Invalid width - greater than max coords', (t) => {
100+
t.plan(1);
101+
t.assert.throws(() => {
102+
sharp().resize(100000001, 240);
103+
}, /Expected positive integer between 1 and 100000000 for width but received 100000001 of type number/);
104+
});
105+
106+
test('Invalid height - greater than max coords', (t) => {
107+
t.plan(1);
108+
t.assert.throws(() => {
109+
sharp().resize(320, 100000001);
110+
}, /Expected positive integer between 1 and 100000000 for height but received 100000001 of type number/);
111+
});
112+
113+
test('Invalid width - greater than max coords - via options', (t) => {
114+
t.plan(1);
115+
t.assert.throws(() => {
116+
sharp().resize({ width: 100000001, height: 240 });
117+
}, /Expected positive integer between 1 and 100000000 for width but received 100000001 of type number/);
118+
});
119+
120+
test('Invalid height - greater than max coords - via options', (t) => {
121+
t.plan(1);
122+
t.assert.throws(() => {
123+
sharp().resize({ width: 320, height: 100000001 });
124+
}, /Expected positive integer between 1 and 100000000 for height but received 100000001 of type number/);
97125
});
98126

99127
test('Invalid width - too large', async (t) => {

0 commit comments

Comments
 (0)