Skip to content

Conversation

Ian-I-Chien
Copy link
Contributor

@Ian-I-Chien Ian-I-Chien commented Jun 24, 2025

fix: reject loadImage when src is null or invalid
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

Thanks for contributing!

  • Have you updated CHANGELOG.md?

Fix #2304

@tom00502
Copy link

I'm also encountering this issue. I hope it can be merged.

@Ian-I-Chien Ian-I-Chien force-pushed the reject-null-for-loadImage branch from cf6b80f to ecd20bd Compare June 30, 2025 06:16
index.js Outdated
@@ -22,6 +22,8 @@ function createImageData (array, width, height) {

function loadImage (src) {
return new Promise((resolve, reject) => {
if (!src) return reject(new Error("Invalid image source"));

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be placed in a final else branch on line 66 of lib/image.js to fix the whole class of bugs. It isn't just null/undefined that will cause onload to not be called. Then you could mark this as fixes #2525 as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @chearon ,
Thanks for the review.
Would you prefer having the check in both index.js and image.js or only in the image.js ?

@Ian-I-Chien Ian-I-Chien force-pushed the reject-null-for-loadImage branch from ecd20bd to 6186db2 Compare August 29, 2025 08:10
@Ian-I-Chien
Copy link
Contributor Author

Hi @chearon ,
I have forced pushed based on your suggestion. Please help review it again.
Thank you.

Ian

@Ian-I-Chien Ian-I-Chien requested a review from chearon September 2, 2025 06:46
@chearon chearon force-pushed the reject-null-for-loadImage branch from 6186db2 to 3f92b48 Compare September 7, 2025 13:42
Copy link
Collaborator

@chearon chearon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks. I just added the issue # to the changelog.

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
```
@chearon chearon force-pushed the reject-null-for-loadImage branch from 3f92b48 to 3fe2c56 Compare September 7, 2025 13:44
@chearon
Copy link
Collaborator

chearon commented Sep 7, 2025

...and fixed conflicts

@chearon chearon merged commit 616859b into Automattic:master Sep 7, 2025
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

loadImage causes the process to terminate
3 participants