Skip to content

Commit 3bc63a3

Browse files
authored
random: Remove RAND_RANGE_BADSCALING (#12374)
This macro is no longer used within php-src since 60ace13, it invokes undefined behavior depending on the input and the corresponding MT_RAND_PHP mode was deprecated in PHP 8.3. Thus remove this macro. Any remaining non-php-src user should just inline it into their code, but should ideally migrate to a non-biased scaler. In any case the undefined behavior of the original implementation should be accounted for.
1 parent f075710 commit 3bc63a3

File tree

2 files changed

+5
-28
lines changed

2 files changed

+5
-28
lines changed

UPGRADING.INTERNALS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ PHP 8.4 INTERNALS UPGRADE NOTES
3636
instead of int.
3737
- The macros DOM_NO_ARGS() and DOM_NOT_IMPLEMENTED() have been removed.
3838

39+
b. ext/random
40+
- The macro RAND_RANGE_BADSCALING() has been removed. The implementation
41+
should either be inlined and undefined behavior fixed or it should be
42+
replaced by a non-biased scaler.
43+
3944
========================
4045
4. OpCode changes
4146
========================

ext/random/php_random.h

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,34 +35,6 @@
3535

3636
PHPAPI double php_combined_lcg(void);
3737

38-
/*
39-
* A bit of tricky math here. We want to avoid using a modulus because
40-
* that simply tosses the high-order bits and might skew the distribution
41-
* of random values over the range. Instead we map the range directly.
42-
*
43-
* We need to map the range from 0...M evenly to the range a...b
44-
* Let n = the random number and n' = the mapped random number
45-
*
46-
* Then we have: n' = a + n(b-a)/M
47-
*
48-
* We have a problem here in that only n==M will get mapped to b which
49-
* means the chances of getting b is much much less than getting any of
50-
* the other values in the range. We can fix this by increasing our range
51-
* artificially and using:
52-
*
53-
* n' = a + n(b-a+1)/M
54-
*
55-
* Now we only have a problem if n==M which would cause us to produce a
56-
* number of b+1 which would be bad. So we bump M up by one to make sure
57-
* this will never happen, and the final algorithm looks like this:
58-
*
59-
* n' = a + n(b-a+1)/(M+1)
60-
*
61-
* -RL
62-
*/
63-
# define RAND_RANGE_BADSCALING(__n, __min, __max, __tmax) \
64-
(__n) = (__min) + (zend_long) ((double) ( (double) (__max) - (__min) + 1.0) * ((__n) / ((__tmax) + 1.0)))
65-
6638
# ifdef PHP_WIN32
6739
# define GENERATE_SEED() (((zend_long) ((zend_ulong) time(NULL) * (zend_ulong) GetCurrentProcessId())) ^ ((zend_long) (1000000.0 * php_combined_lcg())))
6840
# else

0 commit comments

Comments
 (0)