Skip to content

Commit 616859b

Browse files
authored
fix: reject loadImage when src is null or invalid (#2518)
Description: - To handle non-string/non-Buffer sources while fetching image. - Add test cases for '', null, and undefined inputs to loadImage() Test Result: ``` npm run test ... ✔ rejects when loadImage is called with null ✔ rejects when loadImage is called with undefined ✔ rejects when loadImage is called with empty string 291 passing (302ms) 6 pending ```
1 parent 418f555 commit 616859b

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ project adheres to [Semantic Versioning](http://semver.org/).
1111
### Added
1212
### Fixed
1313
* `roundRect()` shape incorrect when radii were large relative to rectangle size (#2400)
14+
* Reject loadImage when src is null or invalid (#2304)
1415

1516
3.2.0
1617
==================

lib/image.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ Object.defineProperty(Image.prototype, 'src', {
6363
}
6464
} else if (Buffer.isBuffer(val)) {
6565
setSource(this, val)
66+
} else {
67+
const err = new Error("Invalid image source")
68+
if (typeof this.onerror === 'function') this.onerror(err)
69+
else throw err
6670
}
6771
},
6872

test/image.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,24 @@ describe('Image', function () {
516516
img.src = path.join(bmpDir, 'bomb.bmp')
517517
})
518518

519+
it('rejects when loadImage is called with null', async function () {
520+
await assert.rejects(
521+
loadImage(null),
522+
)
523+
})
524+
525+
it('rejects when loadImage is called with undefined', async function () {
526+
await assert.rejects(
527+
loadImage(undefined),
528+
)
529+
})
530+
531+
it('rejects when loadImage is called with empty string', async function () {
532+
await assert.rejects(
533+
loadImage(''),
534+
)
535+
})
536+
519537
function testImgd (img, data) {
520538
const ctx = createCanvas(img.width, img.height).getContext('2d')
521539
ctx.drawImage(img, 0, 0)

0 commit comments

Comments
 (0)