Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ AS_VAR_IF([php_cv_func_copy_file_range], [yes],
[AC_DEFINE([HAVE_COPY_FILE_RANGE], [1],
[Define to 1 if you have the 'copy_file_range' function.])])

AC_REPLACE_FUNCS([strlcat strlcpy explicit_bzero getopt])
AC_REPLACE_FUNCS([strlcat strlcpy reallocarray explicit_bzero getopt])
AC_FUNC_ALLOCA
PHP_TIME_R_TYPE

Expand Down Expand Up @@ -1638,6 +1638,7 @@ PHP_ADD_SOURCES([main], m4_normalize([
php_ticks.c
php_variables.c
reentrancy.c
reallocarray.c
rfc1867.c
safe_bcmp.c
SAPI.c
Expand Down
10 changes: 10 additions & 0 deletions main/php.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,16 @@ END_EXTERN_C()
#define explicit_bzero php_explicit_bzero
#endif

#ifndef HAVE_REALLOCARRAY
BEGIN_EXTERN_C()
PHPAPI void *php_reallocarray(void *optr, size_t nmemb, size_t size);
END_EXTERN_C()
#undef reallocarray
#define reallocarray php_reallocarray
#define HAVE_REALLOCARRAY 1
#define USE_REALLOCARRAY_PHP_IMPL 1
#endif

BEGIN_EXTERN_C()
PHPAPI int php_safe_bcmp(const zend_string *a, const zend_string *b);
END_EXTERN_C()
Expand Down
59 changes: 59 additions & 0 deletions main/reallocarray.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should not be in main but in win32. I don't think we will want to use it in this format if glob is made common. In such case we should probably look to introducing something in zend_alloc that would respect memory_limit at some point. I know that the libc glob does not do that but it would be good thing to do eventually.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I imported reallocarray there since it's consistent with other imported utility functions like strlcpy, and we'll need it when we use this glob on not-Windows. ZendMM conversion would render this moot though.

+----------------------------------------------------------------------+
| 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 |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: |
+----------------------------------------------------------------------+
*/

#include "php.h"

#ifdef USE_REALLOCARRAY_PHP_IMPL
/* $OpenBSD: reallocarray.c,v 1.3 2015/09/13 08:31:47 guenther Exp $ */
/*
* Copyright (c) 2008 Otto Moerbeek <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#include <sys/types.h>
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>

/*
* This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
* if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
*/
#define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))

PHPAPI void *
php_reallocarray(void *optr, size_t nmemb, size_t size)
{
if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
nmemb > 0 && SIZE_MAX / nmemb < size) {
errno = ENOMEM;
return NULL;
}
return realloc(optr, size * nmemb);
}

#endif /* !HAVE_REALLOCARRAY */
2 changes: 1 addition & 1 deletion win32/build/config.w32
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ if (VS_TOOLSET && VCVERS >= 1914) {
//AC_DEFINE('ZEND_DVAL_TO_LVAL_CAST_OK', 1);

ADD_SOURCES("main", "main.c snprintf.c spprintf.c getopt.c fopen_wrappers.c \
php_ini_builder.c \
php_ini_builder.c reallocarray.c \
php_scandir.c php_ini.c SAPI.c rfc1867.c php_content_types.c strlcpy.c \
strlcat.c reentrancy.c php_variables.c php_ticks.c network.c \
php_open_temporary_file.c output.c internal_functions.c \
Expand Down
29 changes: 29 additions & 0 deletions win32/charclass.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Public domain, 2008, Todd C. Miller <[email protected]>
*
* $OpenBSD: charclass.h,v 1.3 2020/10/13 04:42:28 guenther Exp $
*/

/*
* POSIX character class support for fnmatch() and glob().
*/
static const struct cclass {
const char *name;
int (*isctype)(int);
} cclasses[] = {
{ "alnum", isalnum },
{ "alpha", isalpha },
{ "blank", isblank },
{ "cntrl", iscntrl },
{ "digit", isdigit },
{ "graph", isgraph },
{ "lower", islower },
{ "print", isprint },
{ "punct", ispunct },
{ "space", isspace },
{ "upper", isupper },
{ "xdigit", isxdigit },
{ NULL, NULL }
};

#define NCCLASSES (sizeof(cclasses) / sizeof(cclasses[0]) - 1)
Loading
Loading