Make WordPress Core

Changeset 53473


Ignore:
Timestamp:
06/06/2022 10:48:21 PM (4 years ago)
Author:
audrasjb
Message:

General: Ensure wp_rand() returns 0 when $min and $max values are equal to 0.

This changeset ensures wp_rand() returns zero instead of a random number when both $min and $max values are equal to zero.

Fixes #55194.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/pluggable.php

    r53472 r53473  
    25972597         * @since 2.6.2
    25982598         * @since 4.4.0 Uses PHP7 random_int() or the random_compat library if available.
     2599         * @since 6.1.0 Returns zero instead of a random number if both `$min` and `$max` are zero.
    25992600         *
    26002601         * @global string $rnd_value
    26012602         *
    2602          * @param int $min Lower limit for the generated number.
    2603          * @param int $max Upper limit for the generated number.
     2603         * @param int $min Optional. Lower limit for the generated number.
     2604         *                 Accepts positive integers or zero. Defaults to 0.
     2605         * @param int $max Optional. Upper limit for the generated number.
     2606         *                 Accepts positive integers. Defaults to 4294967295.
    26042607         * @return int A random number between min and max.
    26052608         */
    2606         function wp_rand( $min = 0, $max = 0 ) {
     2609        function wp_rand( $min = null, $max = null ) {
    26072610                global $rnd_value;
    26082611
    … …  
    26102613                // truncate integers larger than PHP_INT_MAX to PHP_INT_MAX rather than overflowing them to floats.
    26112614                $max_random_number = 3000000000 === 2147483647 ? (float) '4294967295' : 4294967295; // 4294967295 = 0xffffffff
     2615
     2616                if ( null === $min ) {
     2617                        $min = 0;
     2618                }
     2619
     2620                if ( null === $max ) {
     2621                        $max = $max_random_number;
     2622                }
    26122623
    26132624                // We only handle ints, floats are truncated to their integer value.
    … …  
    26192630                if ( $use_random_int_functionality ) {
    26202631                        try {
    2621                                 $_max = ( 0 != $max ) ? $max : $max_random_number;
    26222632                                // wp_rand() can accept arguments in either order, PHP cannot.
    2623                                 $_max = max( $min, $_max );
    2624                                 $_min = min( $min, $_max );
     2633                                $_max = max( $min, $max );
     2634                                $_min = min( $min, $max );
    26252635                                $val  = random_int( $_min, $_max );
    26262636                                if ( false !== $val ) {
    … …  
    26622672
    26632673                // Reduce the value to be within the min - max range.
    2664                 if ( 0 != $max ) {
    2665                         $value = $min + ( $max - $min + 1 ) * $value / ( $max_random_number + 1 );
    2666                 }
     2674                $value = $min + ( $max - $min + 1 ) * $value / ( $max_random_number + 1 );
    26672675
    26682676                return abs( (int) $value );
  • trunk/tests/phpunit/tests/pluggable.php

    r53231 r53473  
    225225                        ),
    226226                        'wp_rand'                         => array(
    227                                 'min' => 0,
    228                                 'max' => 0,
     227                                'min' => null,
     228                                'max' => null,
    229229                        ),
    230230                        'wp_set_password'                 => array( 'password', 'user_id' ),
    … …  
    355355                $this->assertSame( $current_user, $from_get_user_by );
    356356        }
     357
     358        /**
     359         * Tests that wp_rand() returns zero.
     360         *
     361         * @ticket 55194
     362         * @dataProvider data_wp_rand_should_return_zero
     363         * @covers ::wp_rand
     364         *
     365         * @param mixed $min Lower limit for the generated number.
     366         * @param mixed $max Upper limit for the generated number.
     367         */
     368        public function test_wp_rand_should_return_zero( $min, $max ) {
     369                $this->assertSame( 0, wp_rand( $min, $max ) );
     370        }
     371
     372        /**
     373         * Data provider.
     374         *
     375         * @return array
     376         */
     377        public function data_wp_rand_should_return_zero() {
     378                return array(
     379                        'min and max as 0'      => array(
     380                                'min' => 0,
     381                                'max' => 0,
     382                        ),
     383                        'min and max as 0.0'    => array(
     384                                'min' => 0.0,
     385                                'max' => 0.0,
     386                        ),
     387                        'min as null, max as 0' => array(
     388                                'min' => null,
     389                                'max' => 0,
     390                        ),
     391                );
     392        }
     393
     394        /**
     395         * Tests that wp_rand() returns a value between 1 and 99.
     396         *
     397         * @ticket 55194
     398         * @dataProvider data_wp_rand_should_return_between_1_and_99
     399         * @covers ::wp_rand
     400         *
     401         * @param int $min Lower limit for the generated number.
     402         * @param int $max Upper limit for the generated number.
     403         */
     404        public function test_wp_rand_should_return_between_1_and_99( $min, $max ) {
     405                $this->assertGreaterThan(
     406                        0,
     407                        wp_rand( $min, $max ),
     408                        'The value was not greater than 0'
     409                );
     410
     411                $this->assertLessThan(
     412                        100,
     413                        wp_rand( $min, $max ),
     414                        'The value was not less than 100'
     415                );
     416        }
     417
     418        /**
     419         * Data provider.
     420         *
     421         * @return array
     422         */
     423        public function data_wp_rand_should_return_between_1_and_99() {
     424                return array(
     425                        '1 and 99'       => array(
     426                                'min' => 1,
     427                                'max' => 99,
     428                        ),
     429                        '-1 and 99'      => array(
     430                                'min' => -1,
     431                                'max' => 99,
     432                        ),
     433                        '1 and -99'      => array(
     434                                'min' => 1,
     435                                'max' => -99,
     436                        ),
     437                        '-1 and -99'     => array(
     438                                'min' => -1,
     439                                'max' => -99,
     440                        ),
     441                        '1.0 and 99.0'   => array(
     442                                'min' => 1.0,
     443                                'max' => 99.0,
     444                        ),
     445                        '-1.0 and -99.0' => array(
     446                                'min' => -1.0,
     447                                'max' => -99.0,
     448                        ),
     449                );
     450        }
    357451}
Note: See TracChangeset for help on using the changeset viewer.