Skip to content

Commit 03ccf37

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 97a26ae commit 03ccf37

File tree

1 file changed

+15
-56
lines changed

1 file changed

+15
-56
lines changed

win32/glob.c

Lines changed: 15 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -164,47 +164,6 @@ struct glob_path_stat {
164164
zend_stat_t *gps_stat;
165165
};
166166

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

641-
if ((path_stat = calloc(n, sizeof(*path_stat))) == NULL)
600+
if ((path_stat = ecalloc(n, sizeof(*path_stat))) == NULL)
642601
return GLOB_NOSPACE;
643602
for (i = 0; i < n; i++) {
644603
path_stat[i].gps_path = pglob->gl_pathv[o + i];
@@ -649,7 +608,7 @@ static int glob0(const Char *pattern, glob_t *pglob, struct glob_lim *limitp)
649608
pglob->gl_pathv[o + i] = path_stat[i].gps_path;
650609
pglob->gl_statv[o + i] = path_stat[i].gps_stat;
651610
}
652-
free(path_stat);
611+
efree(path_stat);
653612
} else {
654613
qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
655614
pglob->gl_pathc - oldpathc, sizeof(char *),
@@ -869,19 +828,19 @@ static int globextend(const Char *path, glob_t *pglob, struct glob_lim *limitp,
869828
nospace:
870829
for (i = pglob->gl_offs; i < newn - 2; i++) {
871830
if (pglob->gl_pathv && pglob->gl_pathv[i])
872-
free(pglob->gl_pathv[i]);
831+
efree(pglob->gl_pathv[i]);
873832
if ((pglob->gl_flags & GLOB_KEEPSTAT) != 0 &&
874833
pglob->gl_pathv && pglob->gl_pathv[i])
875-
free(pglob->gl_statv[i]);
834+
efree(pglob->gl_statv[i]);
876835
}
877-
free(pglob->gl_pathv);
836+
efree(pglob->gl_pathv);
878837
pglob->gl_pathv = NULL;
879-
free(pglob->gl_statv);
838+
efree(pglob->gl_statv);
880839
pglob->gl_statv = NULL;
881840
return(GLOB_NOSPACE);
882841
}
883842

884-
pathv = reallocarray(pglob->gl_pathv, newn, sizeof(*pathv));
843+
pathv = safe_erealloc_rel(pglob->gl_pathv, newn, sizeof(*pathv), 0);
885844
if (pathv == NULL)
886845
goto nospace;
887846
if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
@@ -893,7 +852,7 @@ static int globextend(const Char *path, glob_t *pglob, struct glob_lim *limitp,
893852
pglob->gl_pathv = pathv;
894853

895854
if ((pglob->gl_flags & GLOB_KEEPSTAT) != 0) {
896-
statv = reallocarray(pglob->gl_statv, newn, sizeof(*statv));
855+
statv = safe_erealloc_rel(pglob->gl_statv, newn, sizeof(*statv), 0);
897856
if (statv == NULL)
898857
goto nospace;
899858
if (pglob->gl_statv == NULL && pglob->gl_offs > 0) {
@@ -913,7 +872,7 @@ static int globextend(const Char *path, glob_t *pglob, struct glob_lim *limitp,
913872
return(GLOB_NOSPACE);
914873
}
915874
if ((statv[pglob->gl_offs + pglob->gl_pathc] =
916-
malloc(sizeof(**statv))) == NULL)
875+
emalloc(sizeof(**statv))) == NULL)
917876
goto copy_error;
918877
memcpy(statv[pglob->gl_offs + pglob->gl_pathc], sb,
919878
sizeof(*sb));
@@ -925,9 +884,9 @@ static int globextend(const Char *path, glob_t *pglob, struct glob_lim *limitp,
925884
;
926885
len = (size_t)(p - path);
927886
limitp->glim_malloc += len;
928-
if ((copy = malloc(len)) != NULL) {
887+
if ((copy = emalloc(len)) != NULL) {
929888
if (g_Ctoc(path, copy, len)) {
930-
free(copy);
889+
efree(copy);
931890
return(GLOB_NOSPACE);
932891
}
933892
pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
@@ -1032,15 +991,15 @@ PHPAPI void globfree(glob_t *pglob)
1032991
if (pglob->gl_pathv != NULL) {
1033992
pp = pglob->gl_pathv + pglob->gl_offs;
1034993
for (i = pglob->gl_pathc; i--; ++pp)
1035-
free(*pp);
1036-
free(pglob->gl_pathv);
994+
efree(*pp);
995+
efree(pglob->gl_pathv);
1037996
pglob->gl_pathv = NULL;
1038997
}
1039998
if (pglob->gl_statv != NULL) {
1040999
for (i = 0; i < pglob->gl_pathc; i++) {
1041-
free(pglob->gl_statv[i]);
1000+
efree(pglob->gl_statv[i]);
10421001
}
1043-
free(pglob->gl_statv);
1002+
efree(pglob->gl_statv);
10441003
pglob->gl_statv = NULL;
10451004
}
10461005
}

0 commit comments

Comments
 (0)