Skip to content

Commit 6d8b9d5

Browse files
committed
ext/random: Add const qualifier
1 parent 853daab commit 6d8b9d5

File tree

6 files changed

+30
-29
lines changed

6 files changed

+30
-29
lines changed

ext/random/engine_mt19937.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,9 @@ static zend_long range(void *state, zend_long min, zend_long max)
171171
}, min, max);
172172
}
173173

174-
static bool serialize(void *state, HashTable *data)
174+
static bool serialize(const void *state, HashTable *data)
175175
{
176-
php_random_status_state_mt19937 *s = state;
176+
const php_random_status_state_mt19937 *s = state;
177177
zval t;
178178

179179
for (uint32_t i = 0; i < N; i++) {
@@ -188,7 +188,7 @@ static bool serialize(void *state, HashTable *data)
188188
return true;
189189
}
190190

191-
static bool unserialize(void *state, HashTable *data)
191+
static bool unserialize(void *state, const HashTable *data)
192192
{
193193
php_random_status_state_mt19937 *s = state;
194194
zval *t;

ext/random/engine_pcgoneseq128xslrr64.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ static zend_long range(void *state, zend_long min, zend_long max)
6363
}, min, max);
6464
}
6565

66-
static bool serialize(void *state, HashTable *data)
66+
static bool serialize(const void *state, HashTable *data)
6767
{
68-
php_random_status_state_pcgoneseq128xslrr64 *s = state;
68+
const php_random_status_state_pcgoneseq128xslrr64 *s = state;
6969
uint64_t u;
7070
zval z;
7171

@@ -80,7 +80,7 @@ static bool serialize(void *state, HashTable *data)
8080
return true;
8181
}
8282

83-
static bool unserialize(void *state, HashTable *data)
83+
static bool unserialize(void *state, const HashTable *data)
8484
{
8585
php_random_status_state_pcgoneseq128xslrr64 *s = state;
8686
uint64_t u[2];

ext/random/engine_xoshiro256starstar.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ static zend_long range(void *state, zend_long min, zend_long max)
118118
}, min, max);
119119
}
120120

121-
static bool serialize(void *state, HashTable *data)
121+
static bool serialize(const void *state, HashTable *data)
122122
{
123-
php_random_status_state_xoshiro256starstar *s = state;
123+
const php_random_status_state_xoshiro256starstar *s = state;
124124
zval t;
125125

126126
for (uint32_t i = 0; i < 4; i++) {
@@ -131,7 +131,7 @@ static bool serialize(void *state, HashTable *data)
131131
return true;
132132
}
133133

134-
static bool unserialize(void *state, HashTable *data)
134+
static bool unserialize(void *state, const HashTable *data)
135135
{
136136
php_random_status_state_xoshiro256starstar *s = state;
137137

ext/random/php_random.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ typedef struct _php_random_algo {
8989
const size_t state_size;
9090
php_random_result (*generate)(void *state);
9191
zend_long (*range)(void *state, zend_long min, zend_long max);
92-
bool (*serialize)(void *state, HashTable *data);
93-
bool (*unserialize)(void *state, HashTable *data);
92+
bool (*serialize)(const void *state, HashTable *data);
93+
bool (*unserialize)(void *state, const HashTable *data);
9494
} php_random_algo;
9595

9696
typedef struct _php_random_algo_with_state {
@@ -149,7 +149,7 @@ static inline php_random_randomizer *php_random_randomizer_from_obj(zend_object
149149
# define Z_RANDOM_RANDOMIZER_P(zval) php_random_randomizer_from_obj(Z_OBJ_P(zval));
150150

151151
PHPAPI void *php_random_status_alloc(const php_random_algo *algo, const bool persistent);
152-
PHPAPI void *php_random_status_copy(const php_random_algo *algo, void *old_status, void *new_status);
152+
PHPAPI void *php_random_status_copy(const php_random_algo *algo, const void *old_status, void *new_status);
153153
PHPAPI void php_random_status_free(void *status, const bool persistent);
154154
PHPAPI php_random_engine *php_random_engine_common_init(zend_class_entry *ce, const php_random_algo *algo);
155155
PHPAPI void php_random_engine_common_free_object(zend_object *object);
@@ -169,7 +169,7 @@ static inline php_random_algo_with_state php_random_default_engine(void)
169169
}
170170

171171
PHPAPI zend_string *php_random_bin2hex_le(const void *ptr, const size_t len);
172-
PHPAPI bool php_random_hex2bin_le(zend_string *hexstr, void *dest);
172+
PHPAPI bool php_random_hex2bin_le(const zend_string *hexstr, void *dest);
173173

174174
PHPAPI void php_random_mt19937_seed32(php_random_status_state_mt19937 *state, uint32_t seed);
175175
PHPAPI void php_random_mt19937_seed_default(php_random_status_state_mt19937 *state);

ext/random/random.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ PHPAPI void *php_random_status_alloc(const php_random_algo *algo, const bool per
240240
return algo->state_size > 0 ? pecalloc(1, algo->state_size, persistent) : NULL;
241241
}
242242

243-
PHPAPI void *php_random_status_copy(const php_random_algo *algo, void *old_status, void *new_status)
243+
PHPAPI void *php_random_status_copy(const php_random_algo *algo, const void *old_status, void *new_status)
244244
{
245245
return memcpy(new_status, old_status, algo->state_size);
246246
}
@@ -354,10 +354,11 @@ PHPAPI zend_string *php_random_bin2hex_le(const void *ptr, const size_t len)
354354

355355
/* {{{ php_random_hex2bin_le */
356356
/* stolen from standard/string.c */
357-
PHPAPI bool php_random_hex2bin_le(zend_string *hexstr, void *dest)
357+
PHPAPI bool php_random_hex2bin_le(const zend_string *hexstr, void *dest)
358358
{
359359
size_t len = hexstr->len >> 1;
360-
unsigned char *str = (unsigned char *) hexstr->val, c, l, d;
360+
const unsigned char *str = (unsigned char *) hexstr->val;
361+
unsigned char c, l, d;
361362
unsigned char *ptr = (unsigned char *) dest;
362363
int is_letter, i = 0;
363364

@@ -461,7 +462,7 @@ PHPAPI zend_long php_mt_rand_range(zend_long min, zend_long max)
461462
* rand() allows min > max, mt_rand does not */
462463
PHPAPI zend_long php_mt_rand_common(zend_long min, zend_long max)
463464
{
464-
php_random_status_state_mt19937 *s = php_random_default_status();
465+
const php_random_status_state_mt19937 *s = php_random_default_status();
465466

466467
if (s->mode == MT_RAND_MT19937) {
467468
return php_mt_rand_range(min, max);
@@ -632,7 +633,7 @@ PHP_FUNCTION(random_int)
632633
}
633634
/* }}} */
634635

635-
static inline void fallback_seed_add(PHP_SHA1_CTX *c, void *p, size_t l){
636+
static inline void fallback_seed_add(PHP_SHA1_CTX *c, const void *p, size_t l){
636637
/* Wrapper around PHP_SHA1Update allowing to pass
637638
* arbitrary pointers without (unsigned char*) casts
638639
* everywhere.

ext/random/randomizer.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ PHP_METHOD(Random_Randomizer, __construct)
8686
/* {{{ Generate a float in [0, 1) */
8787
PHP_METHOD(Random_Randomizer, nextFloat)
8888
{
89-
php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
89+
const php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
9090
php_random_algo_with_state engine = randomizer->engine;
9191

9292
uint64_t result;
@@ -129,7 +129,7 @@ PHP_METHOD(Random_Randomizer, nextFloat)
129129
*/
130130
PHP_METHOD(Random_Randomizer, getFloat)
131131
{
132-
php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
132+
const php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
133133
double min, max;
134134
zend_object *bounds = NULL;
135135
int bounds_type = 'C' + sizeof("ClosedOpen") - 1;
@@ -152,8 +152,8 @@ PHP_METHOD(Random_Randomizer, getFloat)
152152
}
153153

154154
if (bounds) {
155-
zval *case_name = zend_enum_fetch_case_name(bounds);
156-
zend_string *bounds_name = Z_STR_P(case_name);
155+
const zval *case_name = zend_enum_fetch_case_name(bounds);
156+
const zend_string *bounds_name = Z_STR_P(case_name);
157157

158158
bounds_type = ZSTR_VAL(bounds_name)[0] + ZSTR_LEN(bounds_name);
159159
}
@@ -203,7 +203,7 @@ PHP_METHOD(Random_Randomizer, getFloat)
203203
/* {{{ Generate positive random number */
204204
PHP_METHOD(Random_Randomizer, nextInt)
205205
{
206-
php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
206+
const php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
207207
php_random_algo_with_state engine = randomizer->engine;
208208

209209
ZEND_PARSE_PARAMETERS_NONE();
@@ -224,7 +224,7 @@ PHP_METHOD(Random_Randomizer, nextInt)
224224
/* {{{ Generate random number in range */
225225
PHP_METHOD(Random_Randomizer, getInt)
226226
{
227-
php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
227+
const php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
228228
php_random_algo_with_state engine = randomizer->engine;
229229

230230
uint64_t result;
@@ -267,7 +267,7 @@ PHP_METHOD(Random_Randomizer, getInt)
267267
/* {{{ Generate random bytes string in ordered length */
268268
PHP_METHOD(Random_Randomizer, getBytes)
269269
{
270-
php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
270+
const php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
271271
php_random_algo_with_state engine = randomizer->engine;
272272

273273
zend_string *retval;
@@ -342,7 +342,7 @@ PHP_METHOD(Random_Randomizer, getBytes)
342342
/* {{{ Shuffling array */
343343
PHP_METHOD(Random_Randomizer, shuffleArray)
344344
{
345-
php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
345+
const php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
346346
zval *array;
347347

348348
ZEND_PARSE_PARAMETERS_START(1, 1)
@@ -359,7 +359,7 @@ PHP_METHOD(Random_Randomizer, shuffleArray)
359359
/* {{{ Shuffling binary */
360360
PHP_METHOD(Random_Randomizer, shuffleBytes)
361361
{
362-
php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
362+
const php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
363363
zend_string *bytes;
364364

365365
ZEND_PARSE_PARAMETERS_START(1, 1)
@@ -380,7 +380,7 @@ PHP_METHOD(Random_Randomizer, shuffleBytes)
380380
/* {{{ Pick keys */
381381
PHP_METHOD(Random_Randomizer, pickArrayKeys)
382382
{
383-
php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
383+
const php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
384384
zval *input;
385385
zend_long num_req;
386386

@@ -412,7 +412,7 @@ PHP_METHOD(Random_Randomizer, pickArrayKeys)
412412
/* {{{ Get Random Bytes for String */
413413
PHP_METHOD(Random_Randomizer, getBytesFromString)
414414
{
415-
php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
415+
const php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
416416
php_random_algo_with_state engine = randomizer->engine;
417417

418418
zend_long user_length;

0 commit comments

Comments
 (0)