Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't crash when nativeImage.createFromBuffer() is called with invalid buffer (backport: 4-0-x) #17373

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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