Skip to content

Commit 22040f5

Browse files
committed
Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1: Fix undefined behaviour in GENERATE_SEED() Fix undefined behaviour when writing 32-bit values in phar/tar.c
2 parents bf487bd + 6f56c00 commit 22040f5

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

ext/phar/tar.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,13 +1240,15 @@ int phar_tar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int
12401240
return EOF;
12411241
}
12421242
#ifdef WORDS_BIGENDIAN
1243-
# define PHAR_SET_32(var, buffer) \
1244-
*(uint32_t *)(var) = (((((unsigned char*)&(buffer))[3]) << 24) \
1245-
| ((((unsigned char*)&(buffer))[2]) << 16) \
1246-
| ((((unsigned char*)&(buffer))[1]) << 8) \
1247-
| (((unsigned char*)&(buffer))[0]))
1243+
# define PHAR_SET_32(destination, source) do { \
1244+
uint32_t swapped = (((((unsigned char*)&(source))[3]) << 24) \
1245+
| ((((unsigned char*)&(source))[2]) << 16) \
1246+
| ((((unsigned char*)&(source))[1]) << 8) \
1247+
| (((unsigned char*)&(source))[0])); \
1248+
memcpy(destination, &swapped, 4); \
1249+
} while (0);
12481250
#else
1249-
# define PHAR_SET_32(var, buffer) *(uint32_t *)(var) = (uint32_t) (buffer)
1251+
# define PHAR_SET_32(destination, source) memcpy(destination, &source, 4)
12501252
#endif
12511253
PHAR_SET_32(sigbuf, phar->sig_flags);
12521254
PHAR_SET_32(sigbuf + 4, signature_length);

ext/random/php_random.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ PHPAPI double php_combined_lcg(void);
6464
(__n) = (__min) + (zend_long) ((double) ( (double) (__max) - (__min) + 1.0) * ((__n) / ((__tmax) + 1.0)))
6565

6666
# ifdef PHP_WIN32
67-
# define GENERATE_SEED() (((zend_long) (time(0) * GetCurrentProcessId())) ^ ((zend_long) (1000000.0 * php_combined_lcg())))
67+
# define GENERATE_SEED() (((zend_long) ((zend_ulong) time(NULL) * (zend_ulong) GetCurrentProcessId())) ^ ((zend_long) (1000000.0 * php_combined_lcg())))
6868
# else
69-
# define GENERATE_SEED() (((zend_long) (time(0) * getpid())) ^ ((zend_long) (1000000.0 * php_combined_lcg())))
69+
# define GENERATE_SEED() (((zend_long) ((zend_ulong) time(NULL) * (zend_ulong) getpid())) ^ ((zend_long) (1000000.0 * php_combined_lcg())))
7070
# endif
7171

7272
# define PHP_MT_RAND_MAX ((zend_long) (0x7FFFFFFF)) /* (1<<31) - 1 */

0 commit comments

Comments
 (0)