Skip to content

Commit

Permalink
oops: Fix randNumber()
Browse files Browse the repository at this point in the history
This addresses an issue where the `randNumber()` function would crash on
32-Bit systems if the ticket format was set to a really high amount of
digits (eg. ###################). This is because the `max()` value that
was being passed to `mt_rand()` exceeded the `mt_getrandmax()` limit which
caused an error. This updates the function to generate a random number for
each digit to avoid the `mt_getrandmax()` limit.
  • Loading branch information
JediKev committed Feb 21, 2018
1 parent c4669d7 commit 5b8b95a
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions include/class.misc.php
Expand Up @@ -52,12 +52,14 @@ function __rand_seed($value=0) {
}

/* Helper used to generate ticket IDs */
function randNumber($len=6,$start=false,$end=false) {

$start=(!$len && $start)?$start:str_pad(1,$len,"0",STR_PAD_RIGHT);
$end=(!$len && $end)?$end:str_pad(9,$len,"9",STR_PAD_RIGHT);
function randNumber($len=6) {
$number = '';
for ($i=0; $i<$len; $i++) {
$min = ($i == 0) ? 1 : 0;
$number .= mt_rand($min, 9);
}

return mt_rand($start,$end);
return (int) $number;
}

/* misc date helpers...this will go away once we move to php 5 */
Expand Down

0 comments on commit 5b8b95a

Please sign in to comment.