Skip to content

Commit a0568ec

Browse files
committed
Allow values for limitInputPixels larger than 32-bit #3238
1 parent 48e3ea5 commit a0568ec

File tree

5 files changed

+17
-5
lines changed

5 files changed

+17
-5
lines changed

docs/changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ Requires libvips v8.12.2
66

77
### v0.30.6 - TBD
88

9+
* Allow values for `limitInputPixels` larger than 32-bit.
10+
[#3238](https://github.com/lovell/sharp/issues/3238)
11+
912
* Ensure brew-installed `vips` can be detected (regression in 0.30.5).
1013
[#3239](https://github.com/lovell/sharp/issues/3239)
1114

lib/input.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ function _createInputDescriptor (input, inputOptions, containerOptions) {
8686
inputDescriptor.limitInputPixels = inputOptions.limitInputPixels
8787
? Math.pow(0x3FFF, 2)
8888
: 0;
89-
} else if (is.integer(inputOptions.limitInputPixels) && inputOptions.limitInputPixels >= 0) {
89+
} else if (is.integer(inputOptions.limitInputPixels) && is.inRange(inputOptions.limitInputPixels, 0, Number.MAX_SAFE_INTEGER)) {
9090
inputDescriptor.limitInputPixels = inputOptions.limitInputPixels;
9191
} else {
92-
throw is.invalidParameterError('limitInputPixels', 'integer >= 0', inputOptions.limitInputPixels);
92+
throw is.invalidParameterError('limitInputPixels', 'positive integer', inputOptions.limitInputPixels);
9393
}
9494
}
9595
// unlimited

src/common.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ namespace sharp {
4848
int32_t AttrAsInt32(Napi::Object obj, unsigned int const attr) {
4949
return obj.Get(attr).As<Napi::Number>().Int32Value();
5050
}
51+
int64_t AttrAsInt64(Napi::Object obj, std::string attr) {
52+
return obj.Get(attr).As<Napi::Number>().Int64Value();
53+
}
5154
double AttrAsDouble(Napi::Object obj, std::string attr) {
5255
return obj.Get(attr).As<Napi::Number>().DoubleValue();
5356
}
@@ -131,7 +134,7 @@ namespace sharp {
131134
}
132135
}
133136
// Limit input images to a given number of pixels, where pixels = width * height
134-
descriptor->limitInputPixels = AttrAsUint32(input, "limitInputPixels");
137+
descriptor->limitInputPixels = static_cast<uint64_t>(AttrAsInt64(input, "limitInputPixels"));
135138
// Allow switch from random to sequential access
136139
descriptor->access = AttrAsBool(input, "sequentialRead") ? VIPS_ACCESS_SEQUENTIAL : VIPS_ACCESS_RANDOM;
137140
// Remove safety features and allow unlimited SVG/PNG input
@@ -439,7 +442,7 @@ namespace sharp {
439442
}
440443
// Limit input images to a given number of pixels, where pixels = width * height
441444
if (descriptor->limitInputPixels > 0 &&
442-
static_cast<uint64_t>(image.width() * image.height()) > static_cast<uint64_t>(descriptor->limitInputPixels)) {
445+
static_cast<uint64_t>(image.width() * image.height()) > descriptor->limitInputPixels) {
443446
throw vips::VError("Input image exceeds pixel limit");
444447
}
445448
return std::make_tuple(image, imageType);

src/common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ namespace sharp {
4949
std::string file;
5050
char *buffer;
5151
VipsFailOn failOn;
52-
int limitInputPixels;
52+
uint64_t limitInputPixels;
5353
bool unlimited;
5454
VipsAccess access;
5555
size_t bufferLength;

test/unit/io.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,12 @@ describe('Input/output', function () {
706706
});
707707
});
708708

709+
it('Invalid fails - integer overflow', () => {
710+
assert.throws(() => {
711+
sharp({ limitInputPixels: Number.MAX_SAFE_INTEGER + 1 });
712+
});
713+
});
714+
709715
it('Invalid fails - string', () => {
710716
assert.throws(() => {
711717
sharp({ limitInputPixels: 'fail' });

0 commit comments

Comments
 (0)