Make WordPress Core

Ticket #54385: 54385.13.diff

File 54385.13.diff, 4.8 KB (added by adamsilverstein, 3 years ago)
  • src/wp-admin/includes/image.php

    diff --git src/wp-admin/includes/image.php src/wp-admin/includes/image.php
    index d8d5d5b34b..149dfc6fc0 100644
    function wp_generate_attachment_metadata( $attachment_id, $file ) { 
    646646 *
    647647 * @since 2.5.0
    648648 *
    649  * @param string $str
    650  * @return int|float
     649 * @param string $str Fraction string.
     650 * @return string|int|float Returns calculated fraction or originally passed string on failure.
    651651 */
    652652function wp_exif_frac2dec( $str ) {
    653         if ( false === strpos( $str, '/' ) ) {
     653        // Fractions must contain a single `/`.
     654        if ( ! is_scalar( $str ) || substr_count( $str, '/' ) !== 1 ) {
    654655                return $str;
    655656        }
    656657
    657658        list( $numerator, $denominator ) = explode( '/', $str );
    658         if ( ! empty( $denominator ) ) {
    659                 return $numerator / $denominator;
     659
     660        // Both the numerator and the denominator must be numbers.
     661        if ( ! is_numeric( $numerator ) || ! is_numeric( $denominator ) ) {
     662                return $str;
    660663        }
    661         return $str;
     664
     665        // The denominator must not be zero.
     666        if ( empty( $denominator ) ) {
     667                return $str;
     668        }
     669
     670        return $numerator / $denominator;
    662671}
    663672
    664673/**
    function wp_read_image_metadata( $file ) { 
    841850                        $meta['copyright'] = trim( $exif['Copyright'] );
    842851                }
    843852                if ( ! empty( $exif['FNumber'] ) ) {
    844                         $meta['aperture'] = round( wp_exif_frac2dec( $exif['FNumber'] ), 2 );
     853                        $meta['aperture'] = round( (float) wp_exif_frac2dec( $exif['FNumber'] ), 2 );
    845854                }
    846855                if ( ! empty( $exif['Model'] ) ) {
    847856                        $meta['camera'] = trim( $exif['Model'] );
  • tests/phpunit/tests/image/functions.php

    diff --git tests/phpunit/tests/image/functions.php tests/phpunit/tests/image/functions.php
    index 488731a0e8..01bfb9bfcc 100644
    class Tests_Image_Functions extends WP_UnitTestCase { 
    700700                        unlink( $temp_dir . $size['file'] );
    701701                }
    702702        }
     703
     704        /**
     705         * Test for wp_exif_frac2dec verified that it properly handles edge cases
     706         * and always returns an int or float, or original value for failures.
     707         *
     708         * @param string $fraction The fraction to convert.
     709         * @param string $expect   The expected result.
     710         *
     711         * @ticket 54385
     712         * @dataProvider data_wp_exif_frac2dec
     713         *
     714         * @covers ::wp_exif_frac2dec
     715         */
     716        public function test_wp_exif_frac2dec( $fraction, $expect ) {
     717                $this->assertSame( $expect, wp_exif_frac2dec( $fraction ) );
     718        }
     719
     720        /**
     721         * Data provider for testing `wp_exif_frac2dec()`.
     722         *
     723         * @return array {
     724         *     Arguments for testing `wp_exif_frac2dec()`.
     725         */
     726        public function data_wp_exif_frac2dec() {
     727                return array(
     728                        'division by zero is prevented'         => array(
     729                                'fraction' => '0/0',
     730                                'expect'   => '0/0',
     731                        ),
     732                        'division by zero is prevented 2'       => array(
     733                                'fraction' => '100/0',
     734                                'expect'   => '100/0',
     735                        ),
     736                        'typical fnumber'                       => array(
     737                                'fraction' => '4.8',
     738                                'expect'   => '4.8',
     739                        ),
     740                        'typical focal length'                  => array(
     741                                'fraction' => '37 mm',
     742                                'expect'   => '37 mm',
     743                        ),
     744                        'typical exposure time'                 => array(
     745                                'fraction' => '1/350',
     746                                'expect'   => 0.002857142857142857,
     747                        ),
     748                        'division by text prevented'            => array(
     749                                'fraction' => '0/abc',
     750                                'expect'   => '0/abc',
     751                        ),
     752                        'non fraction returns original value'   => array(
     753                                'fraction' => '0.0',
     754                                'expect'   => '0.0',
     755                        ),
     756                        'non fraction returns original value 2' => array(
     757                                'fraction' => '010',
     758                                'expect'   => '010',
     759                        ),
     760                        'non fraction returns original value 3' => array(
     761                                'fraction' => 10.123,
     762                                'expect'   => 10.123,
     763                        ),
     764                        'valid fraction'                        => array(
     765                                'fraction' => '50/100',
     766                                'expect'   => 0.5,
     767                        ),
     768                        'valid fraction 2'                      => array(
     769                                'fraction' => '25/100',
     770                                'expect'   => .25,
     771                        ),
     772                        'valid fraction 3'                      => array(
     773                                'fraction' => '4/2',
     774                                'expect'   => 2,
     775                        ),
     776                        'non fraction returns original value'   => array(
     777                                'fraction' => '0',
     778                                'expect'   => '0',
     779                        ),
     780                        'text is prevented'                     => array(
     781                                'fraction' => 'path/to/file',
     782                                'expect'   => 'path/to/file',
     783                        ),
     784                        'text is prevented 2'                   => array(
     785                                'fraction' => '123notafraction',
     786                                'expect'   => '123notafraction',
     787                        ),
     788                        'invalid input prevented'               => array(
     789                                'fraction' => '/',
     790                                'expect'   => '/',
     791                        ),
     792                        'invalid input prevented 2'             => array(
     793                                'fraction' => '1/2/3',
     794                                'expect'   => '1/2/3',
     795                        ),
     796                        'invalid input prevented 3'             => array(
     797                                'fraction' => '///',
     798                                'expect'   => '///',
     799                        ),
     800                        'null value'                            => array(
     801                                'fraction' => null,
     802                                'expect'   => null,
     803                        ),
     804                        'empty array value'                     => array(
     805                                'fraction' => array(),
     806                                'expect'   => array(),
     807                        ),
     808                );
     809        }
    703810}