#ifndef ZTS
random_globals_dtor(&random_globals);
#endif
+
+ return SUCCESS;
}
/* }}} */
php_error_docref(NULL, E_WARNING, "Could not gather sufficient random data");
return FAILURE;
}
-#else
-#if HAVE_DECL_ARC4RANDOM_BUF
+#elif HAVE_DECL_ARC4RANDOM_BUF
arc4random_buf(bytes, size);
#else
int fd = RANDOM_G(fd);
if (fd < 0) {
#if HAVE_DEV_ARANDOM
fd = open("/dev/arandom", O_RDONLY);
-#else
-#if HAVE_DEV_URANDOM
+#elif HAVE_DEV_URANDOM
fd = open("/dev/urandom", O_RDONLY);
-#endif // URANDOM
-#endif // ARANDOM
+#endif
if (fd < 0) {
php_error_docref(NULL, E_WARNING, "Cannot open source device");
return FAILURE;
php_error_docref(NULL, E_WARNING, "Could not gather sufficient random data");
return FAILURE;
}
-#endif // !ARC4RANDOM_BUF
-#endif // !WIN32
+#endif
return SUCCESS;
}
{
zend_long min;
zend_long max;
- zend_ulong limit;
zend_ulong umax;
zend_ulong result;
RETURN_FALSE;
}
- // Special case where no modulus is required
+ /* Special case where no modulus is required */
if (umax == ZEND_ULONG_MAX) {
RETURN_LONG((zend_long)result);
}
- // Increment the max so the range is inclusive of max
+ /* Increment the max so the range is inclusive of max */
umax++;
- // Powers of two are not biased
+ /* Powers of two are not biased */
if ((umax & ~umax) != umax) {
- // Ceiling under which ZEND_LONG_MAX % max == 0
- limit = ZEND_ULONG_MAX - (ZEND_ULONG_MAX % umax) - 1;
+ /* Ceiling under which ZEND_LONG_MAX % max == 0 */
+ zend_ulong limit = ZEND_ULONG_MAX - (ZEND_ULONG_MAX % umax) - 1;
- // Discard numbers over the limit to avoid modulo bias
+ /* Discard numbers over the limit to avoid modulo bias */
while (result > limit) {
if (php_random_bytes(&result, sizeof(result)) == FAILURE) {
- return;
+ RETURN_FALSE;
}
}
}
--- /dev/null
+--TEST--
+Test error operation of random_bytes()
+--FILE--
+<?php
+//-=-=-=-
+
+var_dump(random_bytes());
+
+var_dump(random_bytes(-1));
+
+?>
+--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)
--- /dev/null
+--TEST--
+Test normal operation of random_int()
+--FILE--
+<?php
+//-=-=-=-
+
+var_dump(is_int(random_int(10, 100)));
+
+$x = random_int(10, 100);
+var_dump($x >= 10 && $x <= 100);
+
+var_dump(random_int(-1000, -1) < 0);
+
+?>
+--EXPECT--
+bool(true)
+bool(true)
+bool(true)
--- /dev/null
+--TEST--
+Test error operation of random_int()
+--FILE--
+<?php
+//-=-=-=-
+
+var_dump(random_int());
+
+var_dump(random_int(10));
+
+var_dump(random_int(10, 0));
+
+?>
+--EXPECTF--
+Warning: random_int() expects exactly 2 parameters, 0 given in %s on line %d
+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)