Skip to content

Commit

Permalink
fix: don't crash when nativeImage.createFromBuffer() called with inva…
Browse files Browse the repository at this point in the history
…lid buffer (#17373)
  • Loading branch information
trop[bot] authored and MarshallOfSound committed Mar 19, 2019
1 parent 61d1df2 commit 5e665c1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 5 additions & 0 deletions atom/common/api/atom_api_native_image.cc
Expand Up @@ -509,6 +509,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.;
Expand Down
7 changes: 6 additions & 1 deletion spec/api-native-image-spec.js
Expand Up @@ -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())
})
Expand Down Expand Up @@ -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)', () => {
Expand Down

0 comments on commit 5e665c1

Please sign in to comment.