Skip to content

Commit e52946e

Browse files
committed
Restrict range of buffer_length on all platforms to INT_MAX
This has only been done for Windows systems so far, and there was a TODO comment about looping for larger values; that appears to be overkill, though, since 2 million bytes should be sufficient for all use cases, and if there is really the need for more, users can still loop manually. Anyhow, checking the range upfront on all platforms is clearer then silently casting to `int`. We split the error message for the least possible BC break. Closes GH-9126.
1 parent 9115211 commit e52946e

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

ext/openssl/openssl.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7642,14 +7642,14 @@ PHP_FUNCTION(openssl_cipher_iv_length)
76427642
PHP_OPENSSL_API zend_string* php_openssl_random_pseudo_bytes(zend_long buffer_length)
76437643
{
76447644
zend_string *buffer = NULL;
7645-
if (buffer_length <= 0
7646-
#ifndef PHP_WIN32
7647-
|| ZEND_LONG_INT_OVFL(buffer_length)
7648-
#endif
7649-
) {
7645+
if (buffer_length <= 0) {
76507646
zend_argument_value_error(1, "must be greater than 0");
76517647
return NULL;
76527648
}
7649+
if (ZEND_LONG_INT_OVFL(buffer_length)) {
7650+
zend_argument_value_error(1, "must be less than 2147483648");
7651+
return NULL;
7652+
}
76537653
buffer = zend_string_alloc(buffer_length, 0);
76547654

76557655
#ifdef PHP_WIN32
@@ -7663,7 +7663,6 @@ PHP_OPENSSL_API zend_string* php_openssl_random_pseudo_bytes(zend_long buffer_le
76637663

76647664
PHP_OPENSSL_CHECK_LONG_TO_INT_NULL_RETURN(buffer_length, length);
76657665
PHP_OPENSSL_RAND_ADD_TIME();
7666-
/* FIXME loop if requested size > INT_MAX */
76677666
if (RAND_bytes((unsigned char*)ZSTR_VAL(buffer), (int)buffer_length) <= 0) {
76687667
zend_string_release_ex(buffer, 0);
76697668
zend_throw_exception(zend_ce_exception, "Error reading from source device", 0);

0 commit comments

Comments
 (0)