From 8928cc56e29cee95507f97ff46c091b9418dd8b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Fri, 3 Mar 2023 17:33:21 +0100 Subject: [PATCH 1/2] random: Convert `php_random_(bytes|int)_(silent|throw)` into inline functions Compared to macros, inline functions are more robust and easier to debug. --- ext/random/php_random.h | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/ext/random/php_random.h b/ext/random/php_random.h index d1057b3a73112..fbbce60ae6cc7 100644 --- a/ext/random/php_random.h +++ b/ext/random/php_random.h @@ -195,14 +195,29 @@ static inline uint64_t php_random_pcgoneseq128xslrr64_rotr64(php_random_uint128_ } # endif -# define php_random_bytes_throw(b, s) php_random_bytes((b), (s), 1) -# define php_random_bytes_silent(b, s) php_random_bytes((b), (s), 0) -# define php_random_int_throw(min, max, result) php_random_int((min), (max), (result), 1) -# define php_random_int_silent(min, max, result) php_random_int((min), (max), (result), 0) - PHPAPI zend_result php_random_bytes(void *bytes, size_t size, bool should_throw); PHPAPI zend_result php_random_int(zend_long min, zend_long max, zend_long *result, bool should_throw); +static inline zend_result php_random_bytes_throw(void *bytes, size_t size) +{ + return php_random_bytes(bytes, size, 1); +} + +static inline zend_result php_random_bytes_silent(void *bytes, size_t size) +{ + return php_random_bytes(bytes, size, 0); +} + +static inline zend_result php_random_int_throw(zend_long min, zend_long max, zend_long *result) +{ + return php_random_int(min, max, result, 1); +} + +static inline zend_result php_random_int_silent(zend_long min, zend_long max, zend_long *result) +{ + return php_random_int(min, max, result, 0); +} + typedef struct _php_random_status_ { size_t last_generated_size; void *state; From 5e8301ccb076e1623bc7fc4992f60cd17e5ebd9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Fri, 3 Mar 2023 20:39:01 +0100 Subject: [PATCH 2/2] random: Use real true/false in `php_random_(bytes|int)_(silent|throw)` --- ext/random/php_random.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ext/random/php_random.h b/ext/random/php_random.h index fbbce60ae6cc7..b51948658e515 100644 --- a/ext/random/php_random.h +++ b/ext/random/php_random.h @@ -200,22 +200,22 @@ PHPAPI zend_result php_random_int(zend_long min, zend_long max, zend_long *resul static inline zend_result php_random_bytes_throw(void *bytes, size_t size) { - return php_random_bytes(bytes, size, 1); + return php_random_bytes(bytes, size, true); } static inline zend_result php_random_bytes_silent(void *bytes, size_t size) { - return php_random_bytes(bytes, size, 0); + return php_random_bytes(bytes, size, false); } static inline zend_result php_random_int_throw(zend_long min, zend_long max, zend_long *result) { - return php_random_int(min, max, result, 1); + return php_random_int(min, max, result, true); } static inline zend_result php_random_int_silent(zend_long min, zend_long max, zend_long *result) { - return php_random_int(min, max, result, 0); + return php_random_int(min, max, result, false); } typedef struct _php_random_status_ {