[wp-trac] [WordPress Trac] #55194: wp_rand( 0, 0) result in random integer

WordPress Trac noreply at wordpress.org
Tue Jun 7 20:31:14 UTC 2022


#55194: wp_rand( 0, 0)  result in random integer
-------------------------------------------------+-------------------------
 Reporter:  mlajo                                |       Owner:  audrasjb
     Type:  defect (bug)                         |      Status:  reopened
 Priority:  normal                               |   Milestone:  6.1
Component:  General                              |     Version:  5.9
 Severity:  normal                               |  Resolution:
 Keywords:  has-patch has-unit-tests needs-dev-  |     Focuses:  docs
  note                                           |
-------------------------------------------------+-------------------------
Changes (by SergeyBiryukov):

 * status:  closed => reopened
 * resolution:  fixed =>


Comment:

 It looks like the tests are not quite correct, this failure now
 [https://github.com/WordPress/wordpress-
 develop/runs/6777919646?check_suite_focus=true#step:20:280 happens
 occasionally]:
 {{{
 1) Tests_Pluggable_wpRand::test_wp_rand_should_return_a_positive_integer
 with data set "-1 and 99" (-1, 99)
 The value was not greater than 0
 Failed asserting that 0 is greater than 0.

 /var/www/tests/phpunit/tests/pluggable/wpRand.php:22
 phpvfscomposer:///var/www/vendor/phpunit/phpunit/phpunit:97
 }}}

 Looking at the test, it makes the assumption that the return value should
 always be greater than zero, due to using `absint()` on the final result.
 However, since [https://www.php.net/manual/en/function.random-int.php
 random_int()] works with negative numbers too, given a range between -1
 and 99, it can return zero as a valid result, which is then returned by
 `wp_rand()` unchanged.

 [attachment:"55194.3.diff"] corrects the affected assertion.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/55194#comment:28>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list