From 63060b5db9a81af4acc9cfaf607d377b21cb787e Mon Sep 17 00:00:00 2001 From: Wouter Verweirder Date: Wed, 10 Dec 2014 09:21:22 +0100 Subject: [PATCH] added checks to raw pixelbuffer size arguments --- lib/obtain.js | 30 +++++++++++++++-- tests/00.argsValidation/001.open.js | 52 +++++++++++++++++++++++++++++ tests/02.operations/001.open.js | 2 +- 3 files changed, 80 insertions(+), 4 deletions(-) diff --git a/lib/obtain.js b/lib/obtain.js index fc7423df..1ddbf00f 100644 --- a/lib/obtain.js +++ b/lib/obtain.js @@ -29,9 +29,33 @@ }); } else if (source instanceof Buffer) { if(typeof type === 'object') { - if(type.width && type.height) { - callback(null, new Image(source, type.width, type.height, type.trans)); - } else throw Error("Missing width and height"); + if(!type.width && !type.height) { + throw Error("Missing width and height"); + } + if(!type.width) { + throw Error("Missing width"); + } + if(!type.height) { + throw Error("Missing height"); + } + if(typeof type.width !== "number") { + throw Error("Width must be numeric"); + } + if(typeof type.height !== "number") { + throw Error("Height must be numeric"); + } + var channelSize = type.width * type.height; + var numChannels = 3; + var size = channelSize * numChannels; + var bufferSize = source.length; + if(size !== bufferSize) { + numChannels++; + size += channelSize; + } + if(size !== bufferSize) { + throw Error("Invalid width or height"); + } + callback(null, new Image(source, type.width, type.height, (numChannels === 4))); } else { var opener = getOpener(type); opener(source, function(err, pixelsBuf, width, height, channels, trans) { diff --git a/tests/00.argsValidation/001.open.js b/tests/00.argsValidation/001.open.js index a5ce9b22..4c10c6da 100644 --- a/tests/00.argsValidation/001.open.js +++ b/tests/00.argsValidation/001.open.js @@ -65,4 +65,56 @@ describe('lwip.open arguments validation', function() { }); + describe('pixelbuffer', function() { + + var buffer; + before(function(done) { + buffer = new Buffer(100 * 100 * 4); + done(); + }); + + describe('without width', function() { + it('should throw an error', function() { + lwip.open.bind(lwip, buffer, { height: 100 }, function() {}).should.throwError(); + }); + }); + + describe('without height', function() { + it('should throw an error', function() { + lwip.open.bind(lwip, buffer, { width: 100 }, function() {}).should.throwError(); + }); + }); + + describe('without width and height', function() { + it('should throw an error', function() { + lwip.open.bind(lwip, buffer, { }, function() {}).should.throwError(); + }); + }); + + describe('with non numeric width', function() { + it('should throw an error', function() { + lwip.open.bind(lwip, buffer, { width: "lorem", height: 100 }, function() {}).should.throwError(); + }); + }); + + describe('with non numeric height', function() { + it('should throw an error', function() { + lwip.open.bind(lwip, buffer, { width: 100, height: "lorem" }, function() {}).should.throwError(); + }); + }); + + describe('with non numeric width and height', function() { + it('should throw an error', function() { + lwip.open.bind(lwip, buffer, { width: "lorem", height: "ipsum" }, function() {}).should.throwError(); + }); + }); + + describe('with incorrect width and height', function() { + it('should throw an error', function() { + lwip.open.bind(lwip, buffer, { width: 123, height: 321 }, function() {}).should.throwError(); + }); + }); + + }); + }); diff --git a/tests/02.operations/001.open.js b/tests/02.operations/001.open.js index c9a4909e..7db6a36f 100644 --- a/tests/02.operations/001.open.js +++ b/tests/02.operations/001.open.js @@ -199,7 +199,7 @@ describe('lwip.open', function() { }); it('should succeed', function(done) { - lwip.open(buffer, { width: 100, height: 100, trans: true }, function(err, img) { + lwip.open(buffer, { width: 100, height: 100 }, function(err, img) { should(err).not.be.Error; img.should.be.OK; done();