Make WordPress Core

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: notglossy's profile notglossy 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)

64902.patch (1.3 KB) - added by yasirkhalifa 5 weeks ago.

Download all attachments as: .zip

Change History (5)

@yasirkhalifa
5 weeks ago

#1 @yasirkhalifa
5 weeks ago

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:

  • Add an explicit is_wp_error() check immediately after the wp_save_image_file() call.
  • Return the error message safely using esc_js() for proper handling in the UI.

This prevents fatal errors and ensures graceful error handling consistent with WordPress coding standards.

Steps to reproduce:

  1. Trigger a failure in the image editor (e.g., unsupported format or editor save failure).
  2. wp_save_image_file() returns WP_Error.
  3. Without this patch → fatal error occurs.
  4. With this patch → error is handled and returned properly.

Feedback welcome. Happy to iterate if needed.

Thanks!

#2 @yasirkhalifa
5 weeks ago

  • Keywords has-patch needs-testing added

#3 follow-up: @aytor
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 @notglossy
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.

Note: See TracTickets for help on using tickets.