fix: don't crash when nativeImage.createFromBuffer() called with invalid buffer (#17344)

This commit is contained in:
Milan Burda 2019-03-13 16:26:11 +01:00 committed by Shelley Vohr
parent 90fcc574f4
commit df7dc9396e
2 changed files with 11 additions and 1 deletions

View file

@ -508,6 +508,11 @@ mate::Handle<NativeImage> NativeImage::CreateFromPath(
mate::Handle<NativeImage> NativeImage::CreateFromBuffer(
mate::Arguments* args,
v8::Local<v8::Value> buffer) {
if (!node::Buffer::HasInstance(buffer)) {
args->ThrowError("buffer must be a node Buffer");
return mate::Handle<NativeImage>();
}
int width = 0;
int height = 0;
double scale_factor = 1.;

View file

@ -127,7 +127,7 @@ describe('nativeImage module', () => {
})
})
describe('createFromBuffer(buffer, scaleFactor)', () => {
describe('createFromBuffer(buffer, options)', () => {
it('returns an empty image when the buffer is empty', () => {
expect(nativeImage.createFromBuffer(Buffer.from([])).isEmpty())
})
@ -165,6 +165,11 @@ describe('nativeImage module', () => {
{ width: 538, height: 190, scaleFactor: 2.0 })
expect(imageI.getSize()).to.deep.equal({ width: 269, height: 95 })
})
it('throws on invalid arguments', () => {
expect(() => nativeImage.createFromBuffer(null)).to.throw('buffer must be a node Buffer')
expect(() => nativeImage.createFromBuffer([12, 14, 124, 12])).to.throw('buffer must be a node Buffer')
})
})
describe('createFromDataURL(dataURL)', () => {