Skip to content

Commit 4195a4a

Browse files
committed
Initial ZendMM conversion of glob
Removes the vendored OpenBSD reallocarray and changes libc memory management to ZendMM. Not sure if non-persistent is the right call, I think glob users use it then call globfree ASAP within a request, but it might be preferable to use persistent allocations.
1 parent efaae93 commit 4195a4a

File tree

2 files changed

+15
-59
lines changed

2 files changed

+15
-59
lines changed

configure.ac

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,6 @@ AC_CHECK_FUNCS(m4_normalize([
572572
poll
573573
pthread_jit_write_protect_np
574574
putenv
575-
reallocarray
576575
scandir
577576
setenv
578577
setitimer

main/php_glob.c

Lines changed: 15 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -172,49 +172,6 @@ struct glob_path_stat {
172172
zend_stat_t *gps_stat;
173173
};
174174

175-
#ifndef HAVE_REALLOCARRAY
176-
/*
177-
* XXX: This is temporary to avoid having reallocarray be imported and part of
178-
* PHP's public API. Since it's only needed here and on Windows, we can just
179-
* put it here for now. Convert this file to ZendMM and remove this function
180-
* when that's complete.
181-
*/
182-
183-
/* $OpenBSD: reallocarray.c,v 1.3 2015/09/13 08:31:47 guenther Exp $ */
184-
/*
185-
* Copyright (c) 2008 Otto Moerbeek <[email protected]>
186-
*
187-
* Permission to use, copy, modify, and distribute this software for any
188-
* purpose with or without fee is hereby granted, provided that the above
189-
* copyright notice and this permission notice appear in all copies.
190-
*
191-
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
192-
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
193-
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
194-
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
195-
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
196-
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
197-
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
198-
*/
199-
200-
/*
201-
* This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
202-
* if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
203-
*/
204-
#define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
205-
206-
static void *
207-
reallocarray(void *optr, size_t nmemb, size_t size)
208-
{
209-
if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
210-
nmemb > 0 && SIZE_MAX / nmemb < size) {
211-
errno = ENOMEM;
212-
return NULL;
213-
}
214-
return realloc(optr, size * nmemb);
215-
}
216-
#endif
217-
218175
static int compare(const void *, const void *);
219176
static int compare_gps(const void *, const void *);
220177
static int g_Ctoc(const Char *, char *, size_t);
@@ -652,7 +609,7 @@ static int glob0(const Char *pattern, php_glob_t *pglob, struct glob_lim *limitp
652609
size_t n = pglob->gl_pathc - oldpathc;
653610
size_t o = pglob->gl_offs + oldpathc;
654611

655-
if ((path_stat = calloc(n, sizeof(*path_stat))) == NULL)
612+
if ((path_stat = ecalloc(n, sizeof(*path_stat))) == NULL)
656613
return PHP_GLOB_NOSPACE;
657614
for (i = 0; i < n; i++) {
658615
path_stat[i].gps_path = pglob->gl_pathv[o + i];
@@ -663,7 +620,7 @@ static int glob0(const Char *pattern, php_glob_t *pglob, struct glob_lim *limitp
663620
pglob->gl_pathv[o + i] = path_stat[i].gps_path;
664621
pglob->gl_statv[o + i] = path_stat[i].gps_stat;
665622
}
666-
free(path_stat);
623+
efree(path_stat);
667624
} else {
668625
qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
669626
pglob->gl_pathc - oldpathc, sizeof(char *),
@@ -883,19 +840,19 @@ static int globextend(const Char *path, php_glob_t *pglob, struct glob_lim *limi
883840
nospace:
884841
for (i = pglob->gl_offs; i < newn - 2; i++) {
885842
if (pglob->gl_pathv && pglob->gl_pathv[i])
886-
free(pglob->gl_pathv[i]);
843+
efree(pglob->gl_pathv[i]);
887844
if ((pglob->gl_flags & PHP_GLOB_KEEPSTAT) != 0 &&
888845
pglob->gl_pathv && pglob->gl_pathv[i])
889-
free(pglob->gl_statv[i]);
846+
efree(pglob->gl_statv[i]);
890847
}
891-
free(pglob->gl_pathv);
848+
efree(pglob->gl_pathv);
892849
pglob->gl_pathv = NULL;
893-
free(pglob->gl_statv);
850+
efree(pglob->gl_statv);
894851
pglob->gl_statv = NULL;
895852
return(PHP_GLOB_NOSPACE);
896853
}
897854

898-
pathv = reallocarray(pglob->gl_pathv, newn, sizeof(*pathv));
855+
pathv = safe_erealloc_rel(pglob->gl_pathv, newn, sizeof(*pathv), 0);
899856
if (pathv == NULL)
900857
goto nospace;
901858
if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
@@ -907,7 +864,7 @@ static int globextend(const Char *path, php_glob_t *pglob, struct glob_lim *limi
907864
pglob->gl_pathv = pathv;
908865

909866
if ((pglob->gl_flags & PHP_GLOB_KEEPSTAT) != 0) {
910-
statv = reallocarray(pglob->gl_statv, newn, sizeof(*statv));
867+
statv = safe_erealloc_rel(pglob->gl_statv, newn, sizeof(*statv), 0);
911868
if (statv == NULL)
912869
goto nospace;
913870
if (pglob->gl_statv == NULL && pglob->gl_offs > 0) {
@@ -927,7 +884,7 @@ static int globextend(const Char *path, php_glob_t *pglob, struct glob_lim *limi
927884
return(PHP_GLOB_NOSPACE);
928885
}
929886
if ((statv[pglob->gl_offs + pglob->gl_pathc] =
930-
malloc(sizeof(**statv))) == NULL)
887+
emalloc(sizeof(**statv))) == NULL)
931888
goto copy_error;
932889
memcpy(statv[pglob->gl_offs + pglob->gl_pathc], sb,
933890
sizeof(*sb));
@@ -939,9 +896,9 @@ static int globextend(const Char *path, php_glob_t *pglob, struct glob_lim *limi
939896
;
940897
len = (size_t)(p - path);
941898
limitp->glim_malloc += len;
942-
if ((copy = malloc(len)) != NULL) {
899+
if ((copy = emalloc(len)) != NULL) {
943900
if (g_Ctoc(path, copy, len)) {
944-
free(copy);
901+
efree(copy);
945902
return(PHP_GLOB_NOSPACE);
946903
}
947904
pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
@@ -1046,15 +1003,15 @@ PHPAPI void php_globfree(php_glob_t *pglob)
10461003
if (pglob->gl_pathv != NULL) {
10471004
pp = pglob->gl_pathv + pglob->gl_offs;
10481005
for (i = pglob->gl_pathc; i--; ++pp)
1049-
free(*pp);
1050-
free(pglob->gl_pathv);
1006+
efree(*pp);
1007+
efree(pglob->gl_pathv);
10511008
pglob->gl_pathv = NULL;
10521009
}
10531010
if (pglob->gl_statv != NULL) {
10541011
for (i = 0; i < pglob->gl_pathc; i++) {
1055-
free(pglob->gl_statv[i]);
1012+
efree(pglob->gl_statv[i]);
10561013
}
1057-
free(pglob->gl_statv);
1014+
efree(pglob->gl_statv);
10581015
pglob->gl_statv = NULL;
10591016
}
10601017
}

0 commit comments

Comments
 (0)