Make WordPress Core

Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#54464 closed defect (bug) (fixed)

Twenty Twenty-One get_attachment_image_attributes_image_attributes not checking if variable isset properly

Reported by: wetah's profile wetah Owned by: audrasjb's profile audrasjb
Milestone: 5.9 Priority: normal
Severity: normal Version:
Component: Bundled Theme Keywords: good-first-bug has-patch commit
Focuses: template Cc:


The twenty_twenty_one_get_attachment_image_attributes function, added to the filter wp_get_attachment_image_attributes in inc/template-functions.php incorrectly checks the existence of some variables in this piece of code:

$meta = wp_get_attachment_metadata( $attachment->ID );
if ( $meta['width'] && $meta['height'] ) {
    $width  = (int) $meta['width'];
    $height = (int) $meta['height'];

This causes an Undefined index: width on line 460 error if the result from the function does not set any value to $meta. I believe something like this should be enough:

$meta = wp_get_attachment_metadata( $attachment->ID );
if ( isset($meta['width']) && isset($meta['height']) ) {
    $width  = (int) $meta['width'];
    $height = (int) $meta['height'];

Attachments (1)

54464.diff (774 bytes) - added by hasanuzzamanshamim 3 years ago.
Patch created for width & height validation

Download all attachments as: .zip

Change History (8)

#1 @SergeyBiryukov
3 years ago

  • Keywords needs-patch good-first-bug added
  • Milestone changed from Awaiting Review to 5.9

3 years ago

Patch created for width & height validation

#2 @hasanuzzamanshamim
3 years ago

  • Keywords has-patch added; needs-patch removed

#3 @mukesh27
3 years ago

  • Keywords commit added

Thanks for the patch!

Mark as commit

#4 @audrasjb
3 years ago

  • Owner set to audrasjb
  • Status changed from new to reviewing

Self-assigning for final review then commit consideration

#5 @audrasjb
3 years ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 52217:

Twenty Twenty-One: Prevent notice thrown in twenty_twenty_one_get_attachment_image_attributes().

Calling twenty_twenty_one_get_attachment_image_attributes() was causing an Undefined index: width|height notice to be thrown when the result from wp_get_attachment_metadata() does not set any value to $meta. This change adds an isset() check to prevent it.

Props wetah, hasanuzzamanshamim.
Fixes #54464.

#6 @audrasjb
3 years ago

#54526 was marked as a duplicate.

#7 @SergeyBiryukov
3 years ago

#54701 was marked as a duplicate.

Note: See TracTickets for help on using tickets.