Make WordPress Core

Ticket #54385: 54385.9.diff

File 54385.9.diff, 2.7 KB (added by TobiasBg, 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..ec24d77c00 100644
    function wp_generate_attachment_metadata( $attachment_id, $file ) { 
    647647 * @since 2.5.0
    648648 *
    649649 * @param string $str
    650  * @return int|float
     650 * @return int|float Returns 0 on failure.
    651651 */
    652652function wp_exif_frac2dec( $str ) {
    653         if ( false === strpos( $str, '/' ) ) {
    654                 return $str;
     653        // Fractions must contain a single `/`.
     654        if ( ! str_contains( $str, '/' ) || substr_count( $str, '/' ) > 1 ) {
     655                return 0;
    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 0;
     663        }
     664
     665        // The denominator must not be empty (or 0).
     666        if ( empty( $denominator ) ) {
     667                return 0;
    660668        }
    661         return $str;
     669
     670        return $numerator / $denominator;
    662671}
    663672
    664673/**
  • tests/phpunit/tests/image/functions.php

    diff --git tests/phpunit/tests/image/functions.php tests/phpunit/tests/image/functions.php
    index 488731a0e8..5ce882d9c9 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, 0 for failures.
     707         *
     708         * @ticket 54385
     709         * @dataProvider data_test_wp_exif_frac2dec
     710         */
     711        public function test_wp_exif_frac2dec( $fraction, $expect ) {
     712                $this->assertSame( $expect, wp_exif_frac2dec( $fraction ) );
     713        }
     714
     715        /**
     716         * Data provider for testing `wp_exif_frac2dec()`.
     717         *
     718         * @return array {
     719         *     Arguments for testing `wp_exif_frac2dec()`.
     720         *
     721         *     @type string|null        $fraction       The fraction string to run.
     722         *     @type int|float       $expected    The resulting expected value.
     723         *
     724         */
     725        public function data_test_wp_exif_frac2dec() {
     726                return array(
     727                        array(
     728                                '0/0',
     729                                0,
     730                        ),
     731                        array(
     732                                '0/abc',
     733                                0,
     734                        ),
     735                        array(
     736                                '0.0',
     737                                0,
     738                        ),
     739                        array(
     740                                '010',
     741                                0,
     742                        ),
     743                        array(
     744                                10.123,
     745                                0,
     746                        ),
     747                        array(
     748                                '50/100',
     749                                0.5,
     750                        ),
     751                        array(
     752                                '25/100',
     753                                .25,
     754                        ),
     755                        array(
     756                                '0',
     757                                0,
     758                        ),
     759                        array(
     760                                '100/0',
     761                                0,
     762                        ),
     763                        array(
     764                                'path/to/file',
     765                                0,
     766                        ),
     767                        array(
     768                                '123notafraction',
     769                                0,
     770                        ),
     771                        array(
     772                                '/',
     773                                0,
     774                        ),
     775                        array(
     776                                '1/2/3',
     777                                0,
     778                        ),
     779                        array(
     780                                '///',
     781                                0,
     782                        ),
     783                        array(
     784                                '4/2',
     785                                2,
     786                        ),
     787                );
     788        }
     789
    703790}