diff --git a/atom/common/api/atom_api_native_image.cc b/atom/common/api/atom_api_native_image.cc index c1dbf59f4ea0..186978448024 100644 --- a/atom/common/api/atom_api_native_image.cc +++ b/atom/common/api/atom_api_native_image.cc @@ -508,6 +508,11 @@ mate::Handle NativeImage::CreateFromPath( mate::Handle NativeImage::CreateFromBuffer( mate::Arguments* args, v8::Local buffer) { + if (!node::Buffer::HasInstance(buffer)) { + args->ThrowError("buffer must be a node Buffer"); + return mate::Handle(); + } + int width = 0; int height = 0; double scale_factor = 1.; diff --git a/spec/api-native-image-spec.js b/spec/api-native-image-spec.js index e60970996971..eb9ab0774275 100644 --- a/spec/api-native-image-spec.js +++ b/spec/api-native-image-spec.js @@ -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)', () => {