Skip to content

Commit be00c05

Browse files
committed
add unsafe RNG handling
1 parent ac8bd68 commit be00c05

File tree

7 files changed

+177
-81
lines changed

7 files changed

+177
-81
lines changed

ext/random/php_random.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,15 @@
8585
# define php_random_int_silent(min, max, result) \
8686
php_random_int((min), (max), (result), 0)
8787

88-
# define RANDOM_ENGINE_GENERATE_SIZE(algo, state, result, generated_size) \
88+
# define RANDOM_ENGINE_GENERATE(algo, state, result, generated_size, rng_unsafe) \
8989
do { \
90-
result = algo->generate(state); \
90+
result = algo->generate(state, rng_unsafe); \
9191
generated_size = algo->static_generate_size != 0 \
9292
? algo->static_generate_size \
9393
: algo->dynamic_generate_size(state) \
9494
; \
9595
} while (0);
96+
# define RANDOM_ENGINE_GENERATE_SILENT(algo, state, result, generate_size) RANDOM_ENGINE_GENERATE(algo, state, result, generated_size, NULL)
9697

9798
PHPAPI double php_combined_lcg(void);
9899

@@ -108,19 +109,19 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, bool should_throw);
108109
PHPAPI int php_random_int(zend_long min, zend_long max, zend_long *result, bool should_throw);
109110

110111
typedef struct _php_random_engine_algo {
111-
const size_t static_generate_size; /* Specifies the generated size. if the generated size is to be changed dynamically to set 0. */
112-
size_t (*dynamic_generate_size)(void *state); /* Pointer to get the dynamic generation size. non-nullable. */
113-
const size_t state_size; /* Size of the structure to hold the state. if not needed to set 0. */
114-
uint64_t (*generate)(void *state); /* Pointer to get random number. non-nullable. */
115-
void (*seed)(void *state, const uint64_t seed); /* Pointer to seeding state. nullable. */
116-
int (*serialize)(void *state, HashTable *data); /* Pointer to serialize state. nullable. */
117-
int (*unserialize)(void *state, HashTable *data); /* Pointer to unserialize state. nullable. */
112+
const size_t static_generate_size; /* Specifies the generated size. if the generated size is to be changed dynamically to set 0. */
113+
size_t (*dynamic_generate_size)(void *state); /* Pointer to get the dynamic generation size. non-nullable. */
114+
const size_t state_size; /* Size of the structure to hold the state. if not needed to set 0. */
115+
uint64_t (*generate)(void *state, bool *rng_unsafe); /* Pointer to get random number. non-nullable. */
116+
void (*seed)(void *state, const uint64_t seed); /* Pointer to seeding state. nullable. */
117+
int (*serialize)(void *state, HashTable *data); /* Pointer to serialize state. nullable. */
118+
int (*unserialize)(void *state, HashTable *data); /* Pointer to unserialize state. nullable. */
118119
} php_random_engine_algo;
119120

120121
PHPAPI const php_random_engine_algo *php_random_engine_get_default_algo(void);
121122
PHPAPI void *php_random_engine_get_default_state(void);
122123

123-
PHPAPI zend_long php_random_engine_range(const php_random_engine_algo *algo, void *state, zend_long min, zend_long max);
124+
PHPAPI zend_long php_random_engine_range(const php_random_engine_algo *algo, void *state, zend_long min, zend_long max, bool *rng_unsafe);
124125

125126
extern zend_module_entry random_module_entry;
126127
# define phpext_random_ptr &random_module_entry

0 commit comments

Comments
 (0)