Skip to content

random: Randomizer::getFloat(): Fix check for empty open intervals #10185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions ext/random/gammasection.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ PHPAPI double php_random_gammasection_closed_open(const php_random_algo *algo, p
{
double g = gamma_max(min, max);
uint64_t hi = ceilint(min, max, g);

if (UNEXPECTED(max <= min || hi < 1)) {
return NAN;
}

uint64_t k = 1 + php_random_range64(algo, status, hi - 1); /* [1, hi] */

if (fabs(min) <= fabs(max)) {
Expand All @@ -79,6 +84,11 @@ PHPAPI double php_random_gammasection_closed_closed(const php_random_algo *algo,
{
double g = gamma_max(min, max);
uint64_t hi = ceilint(min, max, g);

if (UNEXPECTED(max < min)) {
return NAN;
}

uint64_t k = php_random_range64(algo, status, hi); /* [0, hi] */

if (fabs(min) <= fabs(max)) {
Expand All @@ -92,6 +102,11 @@ PHPAPI double php_random_gammasection_open_closed(const php_random_algo *algo, p
{
double g = gamma_max(min, max);
uint64_t hi = ceilint(min, max, g);

if (UNEXPECTED(max <= min || hi < 1)) {
return NAN;
}

uint64_t k = php_random_range64(algo, status, hi - 1); /* [0, hi - 1] */

if (fabs(min) <= fabs(max)) {
Expand All @@ -105,6 +120,11 @@ PHPAPI double php_random_gammasection_open_open(const php_random_algo *algo, php
{
double g = gamma_max(min, max);
uint64_t hi = ceilint(min, max, g);

if (UNEXPECTED(max <= min || hi < 2)) {
return NAN;
}

uint64_t k = 1 + php_random_range64(algo, status, hi - 2); /* [1, hi - 1] */

if (fabs(min) <= fabs(max)) {
Expand Down
9 changes: 8 additions & 1 deletion ext/random/randomizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,14 @@ PHP_METHOD(Random_Randomizer, getFloat)
RETURN_THROWS();
}

RETURN_DOUBLE(php_random_gammasection_open_open(randomizer->algo, randomizer->status, min, max));
RETVAL_DOUBLE(php_random_gammasection_open_open(randomizer->algo, randomizer->status, min, max));

if (UNEXPECTED(isnan(Z_DVAL_P(return_value)))) {
zend_value_error("The given interval is empty, there are no floats between argument #1 ($min) and argument #2 ($max).");
RETURN_THROWS();
}

return;
default:
ZEND_UNREACHABLE();
}
Expand Down
13 changes: 12 additions & 1 deletion ext/random/tests/03_randomizer/methods/getFloat_error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,17 @@ foreach ([
} catch (ValueError $e) {
echo $e->getMessage(), PHP_EOL;
}

try {
// There is no float between the two parameters, thus making the OpenOpen interval empty.
var_dump(randomizer()->getFloat(1.0, 1.0000000000000002, $boundary));
} catch (ValueError $e) {
echo $e->getMessage(), PHP_EOL;
}
}

?>
--EXPECT--
--EXPECTF--
ClosedClosed
Random\Randomizer::getFloat(): Argument #1 ($min) must be finite
Random\Randomizer::getFloat(): Argument #1 ($min) must be finite
Expand All @@ -87,6 +94,7 @@ Random\Randomizer::getFloat(): Argument #2 ($max) must be finite
Random\Randomizer::getFloat(): Argument #2 ($max) must be greater than or equal to argument #1 ($min)
float(0)
float(1.0E+17)
float(%f)
ClosedOpen
Random\Randomizer::getFloat(): Argument #1 ($min) must be finite
Random\Randomizer::getFloat(): Argument #1 ($min) must be finite
Expand All @@ -97,6 +105,7 @@ Random\Randomizer::getFloat(): Argument #2 ($max) must be finite
Random\Randomizer::getFloat(): Argument #2 ($max) must be greater than argument #1 ($min)
Random\Randomizer::getFloat(): Argument #2 ($max) must be greater than argument #1 ($min)
Random\Randomizer::getFloat(): Argument #2 ($max) must be greater than argument #1 ($min)
float(1)
OpenClosed
Random\Randomizer::getFloat(): Argument #1 ($min) must be finite
Random\Randomizer::getFloat(): Argument #1 ($min) must be finite
Expand All @@ -107,6 +116,7 @@ Random\Randomizer::getFloat(): Argument #2 ($max) must be finite
Random\Randomizer::getFloat(): Argument #2 ($max) must be greater than argument #1 ($min)
Random\Randomizer::getFloat(): Argument #2 ($max) must be greater than argument #1 ($min)
Random\Randomizer::getFloat(): Argument #2 ($max) must be greater than argument #1 ($min)
float(1.0000000000000002)
OpenOpen
Random\Randomizer::getFloat(): Argument #1 ($min) must be finite
Random\Randomizer::getFloat(): Argument #1 ($min) must be finite
Expand All @@ -117,3 +127,4 @@ Random\Randomizer::getFloat(): Argument #2 ($max) must be finite
Random\Randomizer::getFloat(): Argument #2 ($max) must be greater than argument #1 ($min)
Random\Randomizer::getFloat(): Argument #2 ($max) must be greater than argument #1 ($min)
Random\Randomizer::getFloat(): Argument #2 ($max) must be greater than argument #1 ($min)
The given interval is empty, there are no floats between argument #1 ($min) and argument #2 ($max).