#63394 closed defect (bug) (duplicate)
preg_match() warning in wp_image_add_srcset_and_sizes() when image_meta['file'] is missing
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | Priority: | normal | |
| Severity: | normal | Version: | |
| Component: | Media | Keywords: | has-patch |
| Focuses: | php-compatibility | Cc: |
Description
In the wp_image_add_srcset_and_sizes() function (wp-includes/media.php), the $image_meta['file'] key is used without checking if it exists.
When image metadata is incomplete or malformed (e.g. missing the file key), this causes the following issues on PHP 8+:
---
### Sample Error (PHP 8+):
Warning: Undefined array key "file" in wp-includes/media.php on line 1768
Deprecated: preg_match(): Passing null to parameter #2 ($subject) of type string is deprecated
### Problem Line (media.php:1768):
if ( preg_match( '/-e[0-9]{13}/', $image_meta['file'], $img_edit_hash ) )
Wrap the logic with a check for 'file' to ensure safe execution:
if ( ! empty( $image_meta['file'] )
&& preg_match( '/-e[0-9]{13}/', $image_meta['file'], $img_edit_hash )
&& ! str_contains( wp_basename( $image_src ), $img_edit_hash[0] ) ) {
return $image;
}
Change History (8)
This ticket was mentioned in PR #8772 on WordPress/wordpress-develop by @ankitkumarshah.
9 months ago
#2
- Keywords has-patch added
#3
@
9 months ago
- Milestone Awaiting Review deleted
- Resolution set to duplicate
- Status changed from new to closed
- Version 6.8 deleted
Hi and welcome to WordPress Core Trac!
#60480 already proposes to check for the 'file' array key in wp_image_add_srcset_and_sizes(). Please continue the discussion on that ticket.
@ankitkumarshah commented on PR #8772:
9 months ago
#4
Hi @narenin, I have updated the PR, please review it at your convenience.
9 months ago
#5
Hi, just to clarify — I originally discovered and documented this issue in Trac Ticket #63394 and proposed a fix.
I also submitted a working PR on my own fork:
https://github.com/Enravo/wordpress-develop/pull/1
The discussion and resolution here (PR #8772) is clearly based on that ticket and should credit the original discovery and proposal.
I'd appreciate it if you could add me to the props list.
GitHub: @Enravo
Thanks!
#6
@
9 months ago
Correction: In my previous comment, "
PR #8772
" was meant to refer to the GitHub pull request:
https://github.com/WordPress/wordpress-develop/pull/8772
Trac automatically linked it to an unrelated internal ticket. Apologies for the confusion.
@ankitkumarshah commented on PR #8772:
9 months ago
#7
Hi, just to clarify — I originally discovered and documented this issue in Trac Ticket #63394 and proposed a fix.
I also submitted a working PR on my own fork: Enravo#1
The discussion and resolution here (PR #8772) is clearly based on that ticket and should credit the original discovery and proposal.
I'd appreciate it if you could add me to the props list. GitHub: @Enravo Thanks!
Hi @Enravo,
The github-actions bot automatically generates the props, and as I can see now, it has added your name in props here: https://github.com/WordPress/wordpress-develop/pull/8772#issuecomment-2853523545. I have also added you in PR description.
@sabernhardt commented on PR #8772:
9 months ago
#8
I mentioned you on Trac 60480, where this effort should continue. You also added a comment there, so you should receive props.
I do not think this PR is the best direction. If the ! empty( $image_meta['file'] ) check is in the same conditional statement as the preg_match(), the wp_image_add_srcset_and_sizes() function might be able to continue without the filename and then pass incomplete metadata to wp_calculate_image_srcset() and/or wp_calculate_image_sizes().
Also, ! empty( $image_meta['file'] ) would cover the case of a missing 'file' key, without needing to add isset( $image_meta['file'] ).
#63394