Make WordPress Core


Ignore:
Timestamp:
12/04/2020 09:39:30 PM (4 years ago)
Author:
iandunn
Message:

Media: Return WP_Error when cropping with bad input to avoid fatal.

This avoids an error on PHP 8 caused by calling wp_imagecreatetruecolor() with inputs that aren't numeric, or are less than 0.

Props hellofromtonya, Boniu91, metalandcoffee, SergeyBiryukov.
Fixes #51937.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-image-editor-gd.php

    r49019 r49751  
    324324        }
    325325
    326         $dst = wp_imagecreatetruecolor( $dst_w, $dst_h );
     326        foreach ( array( $src_w, $src_h, $dst_w, $dst_h ) as $value ) {
     327            if ( ! is_numeric( $value ) || (int) $value <= 0 ) {
     328                return new WP_Error( 'image_crop_error', __( 'Image crop failed.' ), $this->file );
     329            }
     330        }
     331
     332        $dst = wp_imagecreatetruecolor( (int) $dst_w, (int) $dst_h );
    327333
    328334        if ( $src_abs ) {
     
    335341        }
    336342
    337         imagecopyresampled( $dst, $this->image, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h );
     343        imagecopyresampled( $dst, $this->image, 0, 0, (int) $src_x, (int) $src_y, (int) $dst_w, (int) $dst_h, (int) $src_w, (int) $src_h );
    338344
    339345        if ( is_gd_image( $dst ) ) {
Note: See TracChangeset for help on using the changeset viewer.