Opened 5 weeks ago
Last modified 2 days ago
#64902 new defect (bug)
wp_save_image() fatal error when wp_save_image_file() returns WP_Error
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | Awaiting Review | Priority: | normal |
| Severity: | normal | Version: | 6.9.4 |
| Component: | Media | Keywords: | has-patch needs-testing |
| Focuses: | Cc: |
Description
wp_save_image_file() has a return type of array|WP_Error|bool.
wp_save_image() checks the return value of wp_save_image_file() with if ( ! $saved_image ) (line1020), but WP_Error is truthy, so it passes through.
Line 1054 then accesses $saved_image['filesize'] which fatals with "Cannot use object of type WP_Error as array".
This is reproducible when any image editor's save() returns WP_Error (e.g., unsupported format, disk full, encoder failure)
Affects all editors (GD, Imagick, custom) since save() is documented as @return array|WP_Error
This bug was discovered when building a plugin which extends WP_Image_Editor to take advantage of a different graphics library.
Attachments (1)
Change History (5)
#3
follow-up:
↓ 4
@
7 days ago
I tested the bug on wordpress with environemnt:
WordPress: 6.9.4
PHP: 8.2
Local WP
The bug is not reproducing, I'm getting the message saying "Could not load the preview image. Please reload the page and try again." but is not allowing me to save it the button is grayed out.
#4
in reply to:
↑ 3
@
2 days ago
Yes, this is one of the symptoms on the front end since the whole process fails because WP_Error is not an array and returns a 500 error.
Replying to aytor:
I tested the bug on wordpress with environemnt:
WordPress: 6.9.4
PHP: 8.2
Local WP
The bug is not reproducing, I'm getting the message saying "Could not load the preview image. Please reload the page and try again." but is not allowing me to save it the button is grayed out.
Hi,
I’ve attached a patch to properly handle WP_Error returned by wp_save_image_file().
Currently, wp_save_image_file() may return a WP_Error, but wp_save_image() only checks for falsy values. Since WP_Error is a truthy object, execution continues and leads to a fatal error when accessing array keys like $saved_imagefilesize?.
Proposed fix:
This prevents fatal errors and ensures graceful error handling consistent with WordPress coding standards.
Steps to reproduce:
Feedback welcome. Happy to iterate if needed.
Thanks!