diff --git a/CHANGELOG.md b/CHANGELOG.md index b16096f69..aafa96fe8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ project adheres to [Semantic Versioning](http://semver.org/). ### Added ### Fixed * `roundRect()` shape incorrect when radii were large relative to rectangle size (#2400) +* Reject loadImage when src is null or invalid (#2304) 3.2.0 ================== diff --git a/lib/image.js b/lib/image.js index 9ffa3c794..72243439c 100644 --- a/lib/image.js +++ b/lib/image.js @@ -63,6 +63,10 @@ Object.defineProperty(Image.prototype, 'src', { } } else if (Buffer.isBuffer(val)) { setSource(this, val) + } else { + const err = new Error("Invalid image source") + if (typeof this.onerror === 'function') this.onerror(err) + else throw err } }, diff --git a/test/image.test.js b/test/image.test.js index a5d6f415c..edae78beb 100644 --- a/test/image.test.js +++ b/test/image.test.js @@ -516,6 +516,24 @@ describe('Image', function () { img.src = path.join(bmpDir, 'bomb.bmp') }) + it('rejects when loadImage is called with null', async function () { + await assert.rejects( + loadImage(null), + ) + }) + + it('rejects when loadImage is called with undefined', async function () { + await assert.rejects( + loadImage(undefined), + ) + }) + + it('rejects when loadImage is called with empty string', async function () { + await assert.rejects( + loadImage(''), + ) + }) + function testImgd (img, data) { const ctx = createCanvas(img.width, img.height).getContext('2d') ctx.drawImage(img, 0, 0)