Skip to content

Commit

Permalink
fix segfault when onload and onerror are not function
Browse files Browse the repository at this point in the history
  • Loading branch information
willrnch committed Feb 7, 2014
1 parent f811e48 commit 1256ce4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/Image.cc
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -258,7 +258,11 @@ Image::readPNG(void *c, uint8_t *data, unsigned int len) {
NAN_GETTER(Image::GetOnload) { NAN_GETTER(Image::GetOnload) {
NanScope(); NanScope();
Image *img = ObjectWrap::Unwrap<Image>(args.This()); Image *img = ObjectWrap::Unwrap<Image>(args.This());
NanReturnValue(img->onload->GetFunction()); if (img->onload) {
NanReturnValue(img->onload->GetFunction());
} else {
NanReturnNull();
}
} }


/* /*
Expand All @@ -279,7 +283,11 @@ NAN_SETTER(Image::SetOnload) {
NAN_GETTER(Image::GetOnerror) { NAN_GETTER(Image::GetOnerror) {
NanScope(); NanScope();
Image *img = ObjectWrap::Unwrap<Image>(args.This()); Image *img = ObjectWrap::Unwrap<Image>(args.This());
NanReturnValue(img->onerror->GetFunction()); if (img->onerror) {
NanReturnValue(img->onerror->GetFunction());
} else {
NanReturnNull();
}
} }


/* /*
Expand Down
6 changes: 5 additions & 1 deletion test/image.test.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ module.exports = {
var img = new Image var img = new Image
, n = 0; , n = 0;


assert.strictEqual(null, img.onload);

assert.strictEqual(false, img.complete); assert.strictEqual(false, img.complete);
img.onload = function(){ img.onload = function(){
++n; ++n;
Expand All @@ -39,6 +41,8 @@ module.exports = {
, error , error
, n = 0; , n = 0;


assert.strictEqual(null, img.onerror);

assert.strictEqual(false, img.complete); assert.strictEqual(false, img.complete);
img.onload = function(){ img.onload = function(){
assert.fail('called onload'); assert.fail('called onload');
Expand Down Expand Up @@ -77,4 +81,4 @@ module.exports = {


assert.equal(1, n); assert.equal(1, n);
} }
}; };

0 comments on commit 1256ce4

Please sign in to comment.