Skip to content

Commit

Permalink
Allow values for limitInputPixels larger than 32-bit #3238
Browse files Browse the repository at this point in the history
  • Loading branch information
lovell committed May 28, 2022
1 parent 48e3ea5 commit a0568ec
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 5 deletions.
3 changes: 3 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ Requires libvips v8.12.2

### v0.30.6 - TBD

* Allow values for `limitInputPixels` larger than 32-bit.
[#3238](https://github.com/lovell/sharp/issues/3238)

* Ensure brew-installed `vips` can be detected (regression in 0.30.5).
[#3239](https://github.com/lovell/sharp/issues/3239)

Expand Down
4 changes: 2 additions & 2 deletions lib/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ function _createInputDescriptor (input, inputOptions, containerOptions) {
inputDescriptor.limitInputPixels = inputOptions.limitInputPixels
? Math.pow(0x3FFF, 2)
: 0;
} else if (is.integer(inputOptions.limitInputPixels) && inputOptions.limitInputPixels >= 0) {
} else if (is.integer(inputOptions.limitInputPixels) && is.inRange(inputOptions.limitInputPixels, 0, Number.MAX_SAFE_INTEGER)) {
inputDescriptor.limitInputPixels = inputOptions.limitInputPixels;
} else {
throw is.invalidParameterError('limitInputPixels', 'integer >= 0', inputOptions.limitInputPixels);
throw is.invalidParameterError('limitInputPixels', 'positive integer', inputOptions.limitInputPixels);
}
}
// unlimited
Expand Down
7 changes: 5 additions & 2 deletions src/common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ namespace sharp {
int32_t AttrAsInt32(Napi::Object obj, unsigned int const attr) {
return obj.Get(attr).As<Napi::Number>().Int32Value();
}
int64_t AttrAsInt64(Napi::Object obj, std::string attr) {
return obj.Get(attr).As<Napi::Number>().Int64Value();
}
double AttrAsDouble(Napi::Object obj, std::string attr) {
return obj.Get(attr).As<Napi::Number>().DoubleValue();
}
Expand Down Expand Up @@ -131,7 +134,7 @@ namespace sharp {
}
}
// Limit input images to a given number of pixels, where pixels = width * height
descriptor->limitInputPixels = AttrAsUint32(input, "limitInputPixels");
descriptor->limitInputPixels = static_cast<uint64_t>(AttrAsInt64(input, "limitInputPixels"));
// Allow switch from random to sequential access
descriptor->access = AttrAsBool(input, "sequentialRead") ? VIPS_ACCESS_SEQUENTIAL : VIPS_ACCESS_RANDOM;
// Remove safety features and allow unlimited SVG/PNG input
Expand Down Expand Up @@ -439,7 +442,7 @@ namespace sharp {
}
// Limit input images to a given number of pixels, where pixels = width * height
if (descriptor->limitInputPixels > 0 &&
static_cast<uint64_t>(image.width() * image.height()) > static_cast<uint64_t>(descriptor->limitInputPixels)) {
static_cast<uint64_t>(image.width() * image.height()) > descriptor->limitInputPixels) {
throw vips::VError("Input image exceeds pixel limit");
}
return std::make_tuple(image, imageType);
Expand Down
2 changes: 1 addition & 1 deletion src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ namespace sharp {
std::string file;
char *buffer;
VipsFailOn failOn;
int limitInputPixels;
uint64_t limitInputPixels;
bool unlimited;
VipsAccess access;
size_t bufferLength;
Expand Down
6 changes: 6 additions & 0 deletions test/unit/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,12 @@ describe('Input/output', function () {
});
});

it('Invalid fails - integer overflow', () => {
assert.throws(() => {
sharp({ limitInputPixels: Number.MAX_SAFE_INTEGER + 1 });
});
});

it('Invalid fails - string', () => {
assert.throws(() => {
sharp({ limitInputPixels: 'fail' });
Expand Down

0 comments on commit a0568ec

Please sign in to comment.