Skip to content

Tweak CSPRNG error conditions & add exceptions #1398

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
15 changes: 10 additions & 5 deletions ext/standard/random.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <math.h>

#include "php.h"
#include "zend_exceptions.h"
#include "php_random.h"

#if PHP_WIN32
Expand Down Expand Up @@ -132,8 +133,8 @@ PHP_FUNCTION(random_bytes)
}

if (size < 1) {
php_error_docref(NULL, E_WARNING, "Length must be greater than 0");
RETURN_FALSE;
zend_throw_exception(NULL, "Length must be greater than 0", 0);
return;
}

bytes = zend_string_alloc(size, 0);
Expand Down Expand Up @@ -162,9 +163,13 @@ PHP_FUNCTION(random_int)
return;
}

if (min >= max) {
php_error_docref(NULL, E_WARNING, "Minimum value must be less than the maximum value");
RETURN_FALSE;
if (min > max) {
zend_throw_exception(NULL, "Minimum value must be less than or equal to the maximum value", 0);
return;
}

if (min == max) {
RETURN_LONG(min);
}

umax = max - min;
Expand Down
14 changes: 10 additions & 4 deletions ext/standard/tests/random/random_bytes_error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ Test error operation of random_bytes()

var_dump(random_bytes());

var_dump(random_bytes(-1));
$bytes = null;
try {
$bytes = random_bytes(0);
} catch (Exception $e) {
var_dump($e->getMessage());
}
var_dump($bytes);

?>
--EXPECTF--

Warning: random_bytes() expects exactly 1 parameter, 0 given in %s on line %d
NULL

Warning: random_bytes(): Length must be greater than 0 in %s on line %d
bool(false)
string(29) "Length must be greater than 0"
NULL
3 changes: 3 additions & 0 deletions ext/standard/tests/random/random_int.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ var_dump($x >= 10 && $x <= 100);

var_dump(random_int(-1000, -1) < 0);

var_dump(random_int(42, 42));

?>
--EXPECT--
bool(true)
bool(true)
bool(true)
int(42)
13 changes: 9 additions & 4 deletions ext/standard/tests/random/random_int_error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ var_dump(random_int());

var_dump(random_int(10));

var_dump(random_int(10, 0));
$randomInt = null;
try {
$randomInt = random_int(10, 0);
} catch (Exception $e) {
var_dump($e->getMessage());
}
var_dump($randomInt);

?>
--EXPECTF--
Expand All @@ -17,6 +23,5 @@ NULL

Warning: random_int() expects exactly 2 parameters, 1 given in %s on line %d
NULL

Warning: random_int(): Minimum value must be less than the maximum value in %s on line %d
bool(false)
string(61) "Minimum value must be less than or equal to the maximum value"
NULL