Skip to content

Commit a26e92e

Browse files
committed
Add reallocarray to utility functions
From OpenBSD, dependency of newer versions of glob.
1 parent 0837928 commit a26e92e

File tree

4 files changed

+70
-2
lines changed

4 files changed

+70
-2
lines changed

configure.ac

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ AS_VAR_IF([php_cv_func_copy_file_range], [yes],
689689
[AC_DEFINE([HAVE_COPY_FILE_RANGE], [1],
690690
[Define to 1 if you have the 'copy_file_range' function.])])
691691

692-
AC_REPLACE_FUNCS([strlcat strlcpy explicit_bzero getopt])
692+
AC_REPLACE_FUNCS([strlcat strlcpy reallocarray explicit_bzero getopt])
693693
AC_FUNC_ALLOCA
694694
PHP_TIME_R_TYPE
695695

@@ -1638,6 +1638,7 @@ PHP_ADD_SOURCES([main], m4_normalize([
16381638
php_ticks.c
16391639
php_variables.c
16401640
reentrancy.c
1641+
reallocarray.c
16411642
rfc1867.c
16421643
safe_bcmp.c
16431644
SAPI.c

main/php.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,14 @@ END_EXTERN_C()
172172
#define explicit_bzero php_explicit_bzero
173173
#endif
174174

175+
#ifndef HAVE_REALLOCARRAY
176+
BEGIN_EXTERN_C()
177+
PHPAPI void *php_reallocarray(void *optr, size_t nmemb, size_t size);
178+
END_EXTERN_C()
179+
#undef reallocarray
180+
#define reallocarray php_reallocarray
181+
#endif
182+
175183
BEGIN_EXTERN_C()
176184
PHPAPI int php_safe_bcmp(const zend_string *a, const zend_string *b);
177185
END_EXTERN_C()

main/reallocarray.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| Copyright (c) The PHP Group |
4+
+----------------------------------------------------------------------+
5+
| This source file is subject to version 3.01 of the PHP license, |
6+
| that is bundled with this package in the file LICENSE, and is |
7+
| available through the world-wide-web at the following url: |
8+
| https://www.php.net/license/3_01.txt |
9+
| If you did not receive a copy of the PHP license and are unable to |
10+
| obtain it through the world-wide-web, please send a note to |
11+
| license@php.net so we can mail you a copy immediately. |
12+
+----------------------------------------------------------------------+
13+
| Author: |
14+
+----------------------------------------------------------------------+
15+
*/
16+
17+
#include "php.h"
18+
19+
#ifdef USE_REALLOCARRAY_PHP_IMPL
20+
/* $OpenBSD: reallocarray.c,v 1.3 2015/09/13 08:31:47 guenther Exp $ */
21+
/*
22+
* Copyright (c) 2008 Otto Moerbeek <otto@drijf.net>
23+
*
24+
* Permission to use, copy, modify, and distribute this software for any
25+
* purpose with or without fee is hereby granted, provided that the above
26+
* copyright notice and this permission notice appear in all copies.
27+
*
28+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
29+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
30+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
31+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
32+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
33+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
34+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
35+
*/
36+
37+
#include <sys/types.h>
38+
#include <errno.h>
39+
#include <stdint.h>
40+
#include <stdlib.h>
41+
42+
/*
43+
* This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
44+
* if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
45+
*/
46+
#define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
47+
48+
PHPAPI void *
49+
php_reallocarray(void *optr, size_t nmemb, size_t size)
50+
{
51+
if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
52+
nmemb > 0 && SIZE_MAX / nmemb < size) {
53+
errno = ENOMEM;
54+
return NULL;
55+
}
56+
return realloc(optr, size * nmemb);
57+
}
58+
59+
#endif /* !HAVE_REALLOCARRAY */

win32/build/config.w32

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ if (VS_TOOLSET && VCVERS >= 1914) {
284284
//AC_DEFINE('ZEND_DVAL_TO_LVAL_CAST_OK', 1);
285285

286286
ADD_SOURCES("main", "main.c snprintf.c spprintf.c getopt.c fopen_wrappers.c \
287-
php_ini_builder.c \
287+
php_ini_builder.c reallocarray.c \
288288
php_scandir.c php_ini.c SAPI.c rfc1867.c php_content_types.c strlcpy.c \
289289
strlcat.c reentrancy.c php_variables.c php_ticks.c network.c \
290290
php_open_temporary_file.c output.c internal_functions.c \

0 commit comments

Comments
 (0)