Skip to content

Commit 8c16076

Browse files
committed
ext/random: Fix signess issues
1 parent efee76b commit 8c16076

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

ext/random/random.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,10 @@ PHPAPI zend_string *php_random_bin2hex_le(const void *ptr, const size_t len)
338338
i = 0;
339339
#ifdef WORDS_BIGENDIAN
340340
/* force little endian */
341-
for (zend_long j = (len - 1); 0 <= j; j--) {
341+
for (size_t h = len; 0 < h; h--) {
342+
size_t j = h-1;
342343
#else
343-
for (zend_long j = 0; j < len; j++) {
344+
for (size_t j = 0; j < len; j++) {
344345
#endif
345346
ZSTR_VAL(str)[i++] = hexconvtab[((unsigned char *) ptr)[j] >> 4];
346347
ZSTR_VAL(str)[i++] = hexconvtab[((unsigned char *) ptr)[j] & 15];
@@ -362,9 +363,10 @@ PHPAPI bool php_random_hex2bin_le(zend_string *hexstr, void *dest)
362363

363364
#ifdef WORDS_BIGENDIAN
364365
/* force little endian */
365-
for (zend_long j = (len - 1); 0 <= j; j--) {
366+
for (size_t h = len; 0 < h; h--) {
367+
size_t j = h-1;
366368
#else
367-
for (zend_long j = 0; j < len; j++) {
369+
for (size_t j = 0; j < len; j++) {
368370
#endif
369371
c = str[i++];
370372
l = c & ~0x20;
@@ -673,7 +675,7 @@ PHPAPI uint64_t php_random_generate_fallback_seed(void)
673675

674676
uint64_t result = 0;
675677

676-
for (int i = 0; i < sizeof(result); i++) {
678+
for (size_t i = 0; i < sizeof(result); i++) {
677679
result = result | (((uint64_t)RANDOM_G(fallback_seed)[i]) << (i * 8));
678680
}
679681

ext/random/randomizer.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -277,18 +277,19 @@ PHP_METHOD(Random_Randomizer, getBytes)
277277
php_random_algo_with_state engine = randomizer->engine;
278278

279279
zend_string *retval;
280-
zend_long length;
280+
zend_long user_length;
281281
size_t total_size = 0;
282282

283283
ZEND_PARSE_PARAMETERS_START(1, 1)
284-
Z_PARAM_LONG(length)
284+
Z_PARAM_LONG(user_length)
285285
ZEND_PARSE_PARAMETERS_END();
286286

287-
if (length < 1) {
287+
if (user_length < 1) {
288288
zend_argument_value_error(1, "must be greater than 0");
289289
RETURN_THROWS();
290290
}
291291

292+
size_t length = (size_t)user_length;
292293
retval = zend_string_alloc(length, 0);
293294

294295
while (total_size < length) {
@@ -385,13 +386,13 @@ PHP_METHOD(Random_Randomizer, getBytesFromString)
385386
php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
386387
php_random_algo_with_state engine = randomizer->engine;
387388

388-
zend_long length;
389+
zend_long user_length;
389390
zend_string *source, *retval;
390391
size_t total_size = 0;
391392

392393
ZEND_PARSE_PARAMETERS_START(2, 2);
393394
Z_PARAM_STR(source)
394-
Z_PARAM_LONG(length)
395+
Z_PARAM_LONG(user_length)
395396
ZEND_PARSE_PARAMETERS_END();
396397

397398
const size_t source_length = ZSTR_LEN(source);
@@ -402,11 +403,12 @@ PHP_METHOD(Random_Randomizer, getBytesFromString)
402403
RETURN_THROWS();
403404
}
404405

405-
if (length < 1) {
406+
if (user_length < 1) {
406407
zend_argument_value_error(2, "must be greater than 0");
407408
RETURN_THROWS();
408409
}
409410

411+
size_t length = (size_t)user_length;
410412
retval = zend_string_alloc(length, 0);
411413

412414
if (max_offset > 0xff) {

0 commit comments

Comments
 (0)