php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69803 gmp_random_range() modifies second parameter if GMP number
Submitted: 2015-06-11 19:38 UTC Modified: 2015-06-11 19:47 UTC
From: andy dot shawaluk at pkware dot com Assigned: nikic (profile)
Status: Closed Package: GNU MP related
PHP Version: master-Git-2015-06-11 (snap) OS: Windows 7
Private report: No CVE-ID: None
 [2015-06-11 19:38 UTC] andy dot shawaluk at pkware dot com
Description:
------------
When calling the gmp_random_range($min,$max) function where $max is a GMP number, the value of $max is modified - after the function returns, it is reduced by ($min-1).

If $max is an ordinary integer (as shown in the online help page), this does not happen.

Tested with latest Windows Master snapshot (VC11 x86 Thread Safe), retrieved from http://windows.php.net/snapshots/ on 2015/06/11 14:34 CDT.

Revision: r7ec3016 (June 09 2015, 17:20:07)

Test script:
---------------
<?php
$a = gmp_init('100'); $b = gmp_init('200');
echo gmp_strval($a) .", ". gmp_strval($b) ."\n";
$c = gmp_random_range($a, $b);
echo gmp_strval($a) .", ". gmp_strval($b) ." -> ". gmp_strval($c) ."\n";

Expected result:
----------------
100, 200
100, 200 -> [number between 100 and 200 inclusive]

Actual result:
--------------
100, 200
100, 101 -> [number between 100 and 200 inclusive]

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-06-11 19:47 UTC] [email protected]
-Status: Open +Status: Assigned -Assigned To: +Assigned To: nikic
 [2015-06-11 19:47 UTC] [email protected]
Also applies to 5.6: http://3v4l.org/ndepv
 [2015-06-11 20:21 UTC] [email protected]
-Status: Assigned +Status: Closed
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Dec 31 06:00:02 2025 UTC