php-src/ext/random
Tim Düsterhus 13b82eef84
random: Randomizer::getFloat(): Fix check for empty open intervals (#10185)
* random: Randomizer::getFloat(): Fix check for empty open intervals

The check for invalid parameters for the IntervalBoundary::OpenOpen variant was
not correct: If two consecutive doubles are passed as parameters, the resulting
interval is empty, resulting in an uint64 underflow in the γ-section
implementation.

Instead of checking whether `$min < $max`, we must check that there is at least
one more double between `$min` and `$max`, i.e. it must hold that:

	nextafter($min, $max) != $max

Instead of duplicating the comparatively complicated and expensive `nextafter`
logic for a rare error case we instead return `NAN` from the γ-section
implementation when the parameters result in an empty interval and thus underflow.

This allows us to reliably detect this specific error case *after* the fact,
but without modifying the engine state. It also provides reliable error
reporting for other internal functions that might use the γ-section
implementation.

* random: γ-section: Also check that that min is smaller than max

This extends the empty-interval check in the γ-section implementation with a
check that min is actually the smaller of the two parameters.

* random: Use PHP_FLOAT_EPSILON in getFloat_error.phpt

Co-authored-by: Christoph M. Becker <cmbecker69@gmx.de>
2023-01-10 10:16:33 +01:00
..
tests random: Randomizer::getFloat(): Fix check for empty open intervals (#10185) 2023-01-10 10:16:33 +01:00
config.m4 Add Randomizer::nextFloat() and Randomizer::getFloat() (#9679) 2022-12-14 17:48:47 +01:00
config.w32 Add Randomizer::nextFloat() and Randomizer::getFloat() (#9679) 2022-12-14 17:48:47 +01:00
CREDITS
engine_combinedlcg.c Add ext/random Exception hierarchy (#9220) 2022-08-02 20:04:28 +02:00
engine_mt19937.c Fix pre-PHP 8.2 compatibility for php_mt_rand_range() with MT_RAND_PHP (#9839) 2022-10-28 16:52:43 +02:00
engine_pcgoneseq128xslrr64.c random: Validate that the arrays do not contain extra elements when unserializing (#9458) 2022-09-05 17:33:36 +02:00
engine_secure.c Add ext/random Exception hierarchy (#9220) 2022-08-02 20:04:28 +02:00
engine_user.c Add ext/random Exception hierarchy (#9220) 2022-08-02 20:04:28 +02:00
engine_xoshiro256starstar.c random: Validate that the arrays do not contain extra elements when unserializing (#9458) 2022-09-05 17:33:36 +02:00
gammasection.c random: Randomizer::getFloat(): Fix check for empty open intervals (#10185) 2023-01-10 10:16:33 +01:00
php_random.h Add Randomizer::nextFloat() and Randomizer::getFloat() (#9679) 2022-12-14 17:48:47 +01:00
random_arginfo.h Add Randomizer::nextFloat() and Randomizer::getFloat() (#9679) 2022-12-14 17:48:47 +01:00
random.c Merge branch 'PHP-8.2' 2023-01-07 14:03:26 +01:00
random.stub.php Add Randomizer::nextFloat() and Randomizer::getFloat() (#9679) 2022-12-14 17:48:47 +01:00
randomizer.c random: Randomizer::getFloat(): Fix check for empty open intervals (#10185) 2023-01-10 10:16:33 +01:00