From a27089143c6f1b1b695733b36c90510419b77be1 Mon Sep 17 00:00:00 2001 From: Ilija Tovilo Date: Thu, 22 Sep 2022 11:49:20 +0200 Subject: [PATCH] Replace reallocarray with safe_perealloc Fixes GH-9581 --- configure.ac | 4 ++-- main/php.h | 8 -------- main/reallocarray.c | 39 --------------------------------------- sapi/fpm/fpm/fpm_arrays.h | 2 +- 4 files changed, 3 insertions(+), 50 deletions(-) delete mode 100644 main/reallocarray.c diff --git a/configure.ac b/configure.ac index 91df09d1ff19e..7066849c1eb75 100644 --- a/configure.ac +++ b/configure.ac @@ -695,7 +695,7 @@ if test "$ac_cv_func_getaddrinfo" = yes; then AC_DEFINE(HAVE_GETADDRINFO,1,[Define if you have the getaddrinfo function]) fi -AC_REPLACE_FUNCS(strlcat strlcpy explicit_bzero getopt reallocarray) +AC_REPLACE_FUNCS(strlcat strlcpy explicit_bzero getopt) AC_FUNC_ALLOCA PHP_TIME_R_TYPE PHP_CHECK_IN_ADDR_T @@ -1622,7 +1622,7 @@ PHP_ADD_SOURCES(main, main.c snprintf.c spprintf.c \ php_ini_builder.c \ php_ini.c SAPI.c rfc1867.c php_content_types.c strlcpy.c \ strlcat.c explicit_bzero.c reentrancy.c php_variables.c php_ticks.c \ - network.c php_open_temporary_file.c php_odbc_utils.c safe_bcmp.c reallocarray.c \ + network.c php_open_temporary_file.c php_odbc_utils.c safe_bcmp.c \ output.c getopt.c php_syslog.c, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1) PHP_ADD_SOURCES_X(main, fastcgi.c, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1, PHP_FASTCGI_OBJS, no) diff --git a/main/php.h b/main/php.h index 6093d2a932951..b7101ed22f0a6 100644 --- a/main/php.h +++ b/main/php.h @@ -179,14 +179,6 @@ END_EXTERN_C() #define explicit_bzero php_explicit_bzero #endif -#ifndef HAVE_REALLOCARRAY -BEGIN_EXTERN_C() -PHPAPI void* php_reallocarray(void *p, size_t nmb, size_t siz); -END_EXTERN_C() -#undef reallocarray -#define reallocarray php_reallocarray -#endif - BEGIN_EXTERN_C() PHPAPI int php_safe_bcmp(const zend_string *a, const zend_string *b); END_EXTERN_C() diff --git a/main/reallocarray.c b/main/reallocarray.c deleted file mode 100644 index 95500ebdd8591..0000000000000 --- a/main/reallocarray.c +++ /dev/null @@ -1,39 +0,0 @@ -/* - +----------------------------------------------------------------------+ - | Copyright (c) The PHP Group | - +----------------------------------------------------------------------+ - | This source file is subject to version 3.01 of the PHP license, | - | that is bundled with this package in the file LICENSE, and is | - | available through the world-wide-web at the following url: | - | https://www.php.net/license/3_01.txt | - | If you did not receive a copy of the PHP license and are unable to | - | obtain it through the world-wide-web, please send a note to | - | license@php.net so we can mail you a copy immediately. | - +----------------------------------------------------------------------+ - | Author: David Carlier | - +----------------------------------------------------------------------+ -*/ - -#include "php.h" - -#ifndef HAVE_REALLOCARRAY - -#include - -PHPAPI void* php_reallocarray(void *p, size_t nmb, size_t siz) -{ - size_t r; -#ifndef _WIN32 - if (__builtin_mul_overflow(nmb, siz, &r)) { -#else - if (SizeTMult(nmb, siz, &r) != S_OK) { -#endif - // EOVERFLOW may have been, arguably, more appropriate - // but this is what other implementations set - errno = ENOMEM; - return NULL; - } - - return realloc(p, r); -} -#endif diff --git a/sapi/fpm/fpm/fpm_arrays.h b/sapi/fpm/fpm/fpm_arrays.h index 454ff31204b2d..9cdd1e9523523 100644 --- a/sapi/fpm/fpm/fpm_arrays.h +++ b/sapi/fpm/fpm/fpm_arrays.h @@ -86,7 +86,7 @@ static inline void *fpm_array_push(struct fpm_array_s *a) /* {{{ */ if (a->used == a->allocated) { size_t new_allocated = a->allocated ? a->allocated * 2 : 20; - void *new_ptr = reallocarray(a->data, a->sz, new_allocated); + void *new_ptr = safe_perealloc(a->data, a->sz, new_allocated, 0, true); if (!new_ptr) { return 0;