Skip to content

Commit ab8a15c

Browse files
committed
Merge pull request #739 from zbjornson/674-clamp-buf
Clamp JPEG buffer size. Fixes #674
2 parents 965908e + 6edfe44 commit ab8a15c

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

lib/canvas.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,11 @@ Canvas.prototype.createJPEGStream = function(options){
185185
Canvas.prototype.syncJPEGStream =
186186
Canvas.prototype.createSyncJPEGStream = function(options){
187187
options = options || {};
188+
// Don't allow the buffer size to exceed the size of the canvas (#674)
189+
var maxBufSize = this.width * this.height * 4;
190+
var clampedBufSize = Math.min(options.bufsize || 4096, maxBufSize);
188191
return new JPEGStream(this, {
189-
bufsize: options.bufsize || 4096
192+
bufsize: clampedBufSize
190193
, quality: options.quality || 75
191194
, progressive: options.progressive || false
192195
});

test/canvas.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,4 +786,14 @@ describe('Canvas', function () {
786786
done(err);
787787
});
788788
});
789+
790+
it('Canvas#jpegStream() should clamp buffer size (#674)', function (done) {
791+
var c = new Canvas(10, 10);
792+
var SIZE = 10 * 1024 * 1024;
793+
var s = c.jpegStream({bufsize: SIZE});
794+
s.on('data', function (chunk) {
795+
assert(chunk.length < SIZE);
796+
});
797+
s.on('end', done);
798+
})
789799
});

0 commit comments

Comments
 (0)