Skip to content

Check for __builtin_mul_overflow before using it #9573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions build/php.m4
Original file line number Diff line number Diff line change
Expand Up @@ -2567,6 +2567,27 @@ AC_DEFUN([PHP_CHECK_BUILTIN_CTZLL], [
AC_DEFINE_UNQUOTED([PHP_HAVE_BUILTIN_CTZLL], [$have_builtin_ctzll], [Whether the compiler supports __builtin_ctzll])
])

dnl
dnl PHP_CHECK_BUILTIN_MUL_OVERFLOW
dnl
AC_DEFUN([PHP_CHECK_BUILTIN_MUL_OVERFLOW], [
AC_MSG_CHECKING([for __builtin_mul_overflow])

AC_LINK_IFELSE([AC_LANG_PROGRAM([], [[
long tmpvar;
return __builtin_mul_overflow(3, 7, &tmpvar);
]])], [
have_builtin_mul_overflow=1
AC_MSG_RESULT([yes])
], [
have_builtin_mul_overflow=0
AC_MSG_RESULT([no])
])

AC_DEFINE_UNQUOTED([PHP_HAVE_BUILTIN_MUL_OVERFLOW],
[$have_builtin_mul_overflow], [Whether the compiler supports __builtin_mul_overflow])
])

dnl
dnl PHP_CHECK_BUILTIN_SMULL_OVERFLOW
dnl
Expand Down
2 changes: 2 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,8 @@ dnl Check __builtin_ctzl
PHP_CHECK_BUILTIN_CTZL
dnl Check __builtin_ctzll
PHP_CHECK_BUILTIN_CTZLL
dnl Check __builtin_mul_overflow
PHP_CHECK_BUILTIN_MUL_OVERFLOW
dnl Check __builtin_smull_overflow
PHP_CHECK_BUILTIN_SMULL_OVERFLOW
dnl Check __builtin_smulll_overflow
Expand Down
7 changes: 5 additions & 2 deletions main/reallocarray.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@
PHPAPI void* php_reallocarray(void *p, size_t nmb, size_t siz)
{
size_t r;
#ifndef _WIN32
#ifdef _WIN32
if (SizeTMult(nmb, siz, &r) != S_OK) {
#elif PHP_HAVE_BUILTIN_MUL_OVERFLOW
if (__builtin_mul_overflow(nmb, siz, &r)) {
#else
if (SizeTMult(nmb, siz, &r) != S_OK) {
r = siz * nmb;
if (siz != 0 && r / siz != nmb) {
#endif
// EOVERFLOW may have been, arguably, more appropriate
// but this is what other implementations set
Expand Down