From 7515fcedbe44ae7573dae8337f6d333803c662a2 Mon Sep 17 00:00:00 2001 From: kibao Date: Thu, 30 Apr 2015 19:35:53 +0200 Subject: [PATCH 1/2] Don't create image data Buffer when data is empty Fix for segmentation fault when image couldn't be loaded --- src/node.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/node.js b/src/node.js index c00879cc..8b3a903a 100644 --- a/src/node.js +++ b/src/node.js @@ -71,10 +71,12 @@ fabric.util.loadImage = function(url, callback, context) { function createImageAndCallBack(data) { - img.src = new Buffer(data, 'binary'); - // preserving original url, which seems to be lost in node-canvas - img._src = url; - callback && callback.call(context, img); + if (data) { + img.src = new Buffer(data, 'binary'); + // preserving original url, which seems to be lost in node-canvas + img._src = url; + callback && callback.call(context, img); + } } var img = new Image(); if (url && (url instanceof Buffer || url.indexOf('data') === 0)) { From 69d616ad2371bee9a930cd0a8351975c96606429 Mon Sep 17 00:00:00 2001 From: kibao Date: Wed, 6 May 2015 21:34:41 +0200 Subject: [PATCH 2/2] Node.js Invoke loadImage callback if there is no data or a network error --- src/node.js | 5 +++++ test/unit/util.js | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/node.js b/src/node.js index 8b3a903a..e82c3d0c 100644 --- a/src/node.js +++ b/src/node.js @@ -50,6 +50,7 @@ else { fabric.log(err.message); } + callback(null); }); req.end(); @@ -77,6 +78,10 @@ img._src = url; callback && callback.call(context, img); } + else { + img = null; + callback && callback.call(context, null, true); + } } var img = new Image(); if (url && (url instanceof Buffer || url.indexOf('data') === 0)) { diff --git a/test/unit/util.js b/test/unit/util.js index 11e29b5c..13708b84 100644 --- a/test/unit/util.js +++ b/test/unit/util.js @@ -24,6 +24,8 @@ ? require("path").join(__dirname, '../fixtures/', 'very_large_image.jpg') : getAbsolutePath('../fixtures/very_large_image.jpg'); + var IMG_URL_NON_EXISTING = 'http://www.google.com/non-existing'; + test('fabric.util.toFixed', function(){ ok(typeof fabric.util.toFixed == 'function'); @@ -459,6 +461,23 @@ }, 1000); }); + + asyncTest('fabric.util.loadImage with url for a non exsiting image', function() { + var callbackInvoked = false; + var hadError = false; + + fabric.util.loadImage(IMG_URL_NON_EXISTING, function(img, error) { + callbackInvoked = true; + hadError = error; + }); + + setTimeout(function() { + ok(callbackInvoked, 'callback should be invoked'); + equal(hadError, true, 'callback should be invoked with error set to true'); + start(); + }, 1000); + }); + var SVG_WITH_1_ELEMENT = '\ \ \