Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 275c852

Browse files
donmahallempatrickhulce
authored andcommitted
fix: more descriptive error for exceeding maxLength buffer (jpeg-js#62)
1 parent d00366a commit 275c852

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

lib/decoder.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,13 +1016,23 @@ function decode(jpegData, opts) {
10161016
decoder.colorTransform = opts.colorTransform;
10171017

10181018
var channels = (opts.formatAsRGBA) ? 4 : 3;
1019-
var image = {
1020-
width: decoder.width,
1021-
height: decoder.height,
1022-
data: opts.useTArray ?
1023-
new Uint8Array(decoder.width * decoder.height * channels) :
1024-
new Buffer(decoder.width * decoder.height * channels)
1025-
};
1019+
var bytesNeeded = decoder.width * decoder.height * channels;
1020+
try {
1021+
var image = {
1022+
width: decoder.width,
1023+
height: decoder.height,
1024+
data: opts.useTArray ?
1025+
new Uint8Array(bytesNeeded) :
1026+
new Buffer(bytesNeeded)
1027+
};
1028+
} catch (err){
1029+
if (err instanceof RangeError){
1030+
throw new Error("Could not allocate enough memory for the image. " +
1031+
"Required: " + bytesNeeded);
1032+
} else {
1033+
throw err;
1034+
}
1035+
}
10261036

10271037
decoder.copyToImageData(image, opts.formatAsRGBA);
10281038

0 commit comments

Comments
 (0)