Skip to content

Commit dd26926

Browse files
SammyKnikic
authored andcommitted
Add tests for CSPRNG, fix C99 comments
Also replace one return; with RETURN_FALSE; for consistency.
1 parent 5f1b83e commit dd26926

File tree

5 files changed

+84
-16
lines changed

5 files changed

+84
-16
lines changed

ext/standard/random.c

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ PHP_MSHUTDOWN_FUNCTION(random)
6868
#ifndef ZTS
6969
random_globals_dtor(&random_globals);
7070
#endif
71+
72+
return SUCCESS;
7173
}
7274
/* }}} */
7375

@@ -80,8 +82,7 @@ static int php_random_bytes(void *bytes, size_t size)
8082
php_error_docref(NULL, E_WARNING, "Could not gather sufficient random data");
8183
return FAILURE;
8284
}
83-
#else
84-
#if HAVE_DECL_ARC4RANDOM_BUF
85+
#elif HAVE_DECL_ARC4RANDOM_BUF
8586
arc4random_buf(bytes, size);
8687
#else
8788
int fd = RANDOM_G(fd);
@@ -90,11 +91,9 @@ static int php_random_bytes(void *bytes, size_t size)
9091
if (fd < 0) {
9192
#if HAVE_DEV_ARANDOM
9293
fd = open("/dev/arandom", O_RDONLY);
93-
#else
94-
#if HAVE_DEV_URANDOM
94+
#elif HAVE_DEV_URANDOM
9595
fd = open("/dev/urandom", O_RDONLY);
96-
#endif // URANDOM
97-
#endif // ARANDOM
96+
#endif
9897
if (fd < 0) {
9998
php_error_docref(NULL, E_WARNING, "Cannot open source device");
10099
return FAILURE;
@@ -115,8 +114,7 @@ static int php_random_bytes(void *bytes, size_t size)
115114
php_error_docref(NULL, E_WARNING, "Could not gather sufficient random data");
116115
return FAILURE;
117116
}
118-
#endif // !ARC4RANDOM_BUF
119-
#endif // !WIN32
117+
#endif
120118

121119
return SUCCESS;
122120
}
@@ -157,7 +155,6 @@ PHP_FUNCTION(random_int)
157155
{
158156
zend_long min;
159157
zend_long max;
160-
zend_ulong limit;
161158
zend_ulong umax;
162159
zend_ulong result;
163160

@@ -176,23 +173,23 @@ PHP_FUNCTION(random_int)
176173
RETURN_FALSE;
177174
}
178175

179-
// Special case where no modulus is required
176+
/* Special case where no modulus is required */
180177
if (umax == ZEND_ULONG_MAX) {
181178
RETURN_LONG((zend_long)result);
182179
}
183180

184-
// Increment the max so the range is inclusive of max
181+
/* Increment the max so the range is inclusive of max */
185182
umax++;
186183

187-
// Powers of two are not biased
184+
/* Powers of two are not biased */
188185
if ((umax & ~umax) != umax) {
189-
// Ceiling under which ZEND_LONG_MAX % max == 0
190-
limit = ZEND_ULONG_MAX - (ZEND_ULONG_MAX % umax) - 1;
186+
/* Ceiling under which ZEND_LONG_MAX % max == 0 */
187+
zend_ulong limit = ZEND_ULONG_MAX - (ZEND_ULONG_MAX % umax) - 1;
191188

192-
// Discard numbers over the limit to avoid modulo bias
189+
/* Discard numbers over the limit to avoid modulo bias */
193190
while (result > limit) {
194191
if (php_random_bytes(&result, sizeof(result)) == FAILURE) {
195-
return;
192+
RETURN_FALSE;
196193
}
197194
}
198195
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
Test normal operation of random_bytes()
3+
--FILE--
4+
<?php
5+
//-=-=-=-
6+
7+
var_dump(strlen(bin2hex(random_bytes(16))));
8+
9+
var_dump(is_string(random_bytes(10)));
10+
11+
?>
12+
--EXPECT--
13+
int(32)
14+
bool(true)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Test error operation of random_bytes()
3+
--FILE--
4+
<?php
5+
//-=-=-=-
6+
7+
var_dump(random_bytes());
8+
9+
var_dump(random_bytes(-1));
10+
11+
?>
12+
--EXPECTF--
13+
Warning: random_bytes() expects exactly 1 parameter, 0 given in %s on line %d
14+
NULL
15+
16+
Warning: random_bytes(): Length must be greater than 0 in %s on line %d
17+
bool(false)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Test normal operation of random_int()
3+
--FILE--
4+
<?php
5+
//-=-=-=-
6+
7+
var_dump(is_int(random_int(10, 100)));
8+
9+
$x = random_int(10, 100);
10+
var_dump($x >= 10 && $x <= 100);
11+
12+
var_dump(random_int(-1000, -1) < 0);
13+
14+
?>
15+
--EXPECT--
16+
bool(true)
17+
bool(true)
18+
bool(true)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
Test error operation of random_int()
3+
--FILE--
4+
<?php
5+
//-=-=-=-
6+
7+
var_dump(random_int());
8+
9+
var_dump(random_int(10));
10+
11+
var_dump(random_int(10, 0));
12+
13+
?>
14+
--EXPECTF--
15+
Warning: random_int() expects exactly 2 parameters, 0 given in %s on line %d
16+
NULL
17+
18+
Warning: random_int() expects exactly 2 parameters, 1 given in %s on line %d
19+
NULL
20+
21+
Warning: random_int(): Minimum value must be less than the maximum value in %s on line %d
22+
bool(false)

0 commit comments

Comments
 (0)