1
0
mirror of https://github.com/php/php-src.git synced 2026-03-28 02:02:32 +01:00

Avoid shift UB for large arrays

Don't shift into the sign bit.
This commit is contained in:
Nikita Popov
2020-01-28 15:33:33 +01:00
parent 7db3a51884
commit d91b166c3a

View File

@@ -102,14 +102,14 @@ static zend_always_inline uint32_t zend_hash_check_size(uint32_t nSize)
#if defined(ZEND_WIN32)
if (BitScanReverse(&index, nSize - 1)) {
return 0x2 << ((31 - index) ^ 0x1f);
return 0x2u << ((31 - index) ^ 0x1f);
} else {
/* nSize is ensured to be in the valid range, fall back to it
rather than using an undefined bis scan result. */
return nSize;
}
#elif (defined(__GNUC__) || __has_builtin(__builtin_clz)) && defined(PHP_HAVE_BUILTIN_CLZ)
return 0x2 << (__builtin_clz(nSize - 1) ^ 0x1f);
return 0x2u << (__builtin_clz(nSize - 1) ^ 0x1f);
#else
nSize -= 1;
nSize |= (nSize >> 1);