Skip to content

Add checks before using size data in image_get_intermediate_size()#3143

Open
remyperona wants to merge 1 commit intoWordPress:trunkfrom
wp-media:54943
Open

Add checks before using size data in image_get_intermediate_size()#3143
remyperona wants to merge 1 commit intoWordPress:trunkfrom
wp-media:54943

Conversation

@remyperona
Copy link

The size data is used in the foreach loop without checking if it's an array as expected, and also without checking the existence of the width and height keys.

Also renamed the $data variable to $size_data, as the function already as a $data variable declared before which is holding different values, and it can be confusing.

Trac ticket: https://core.trac.wordpress.org/ticket/54943


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.


foreach ( $imagedata['sizes'] as $_size => $data ) {
foreach ( $imagedata['sizes'] as $_size => $size_data ) {
if ( ! is_array( $size_data ) || ! isset( $size_data['width'] ) || ! isset( $size_data['height'] ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

is_array() isn't needed here as the isset() is checking it.

foreach ( $imagedata['sizes'] as $_size => $data ) {
foreach ( $imagedata['sizes'] as $_size => $size_data ) {
if ( ! is_array( $size_data ) || ! isset( $size_data['width'] ) || ! isset( $size_data['height'] ) ) {
continue;
Copy link
Contributor

Choose a reason for hiding this comment

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

Silently skipping isn't preferred here. Why?

wp_get_attachment_metadata() returns an array on success and 'sizes' is expected to be an array. If it's not an array, then something went wrong. Silently skipping makes it harder for developers to debug what happened and could lead to confusion for users.

Copy link
Contributor

@hellofromtonya hellofromtonya left a comment

Choose a reason for hiding this comment

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

Thank you @Tabrisrp for submitting this PR.

The code expects the 'size' element to be an array as documented in wp_get_attachment_metadata().

While this PR adds defensive guards to protect it, it is silently skipping when 'sizes' is not an array or the 'width' or 'height' is not defined. I'm all for defensive coding.

But I wonder: Why are there no sizes for the attachment(s)? Could the issue be a callback hooked into filter? Or maybe the metadata is incomplete? Or did something happen when the attachment was added or edited?

Knowing the root cause will help contributors to determine if there's a bug to fix in Core, if defensive checks are needed here, and if an error or notice should be displayed to alert users of a problem with their attachment or developers with their code.

Also, the PR will need tests.

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.

2 participants