safer realloc, from openbsd

This commit is contained in:
itojun 2003-11-07 04:44:57 +00:00
parent 552d27479a
commit 434c034061
1 changed files with 16 additions and 10 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: process.c,v 1.34 2003/11/07 03:58:06 itojun Exp $ */
/* $NetBSD: process.c,v 1.35 2003/11/07 04:44:57 itojun Exp $ */
/*-
* Copyright (c) 1992, 1993, 1994
@ -72,7 +72,7 @@
#if 0
static char sccsid[] = "@(#)process.c 8.6 (Berkeley) 4/20/94";
#else
__RCSID("$NetBSD: process.c,v 1.34 2003/11/07 03:58:06 itojun Exp $");
__RCSID("$NetBSD: process.c,v 1.35 2003/11/07 04:44:57 itojun Exp $");
#endif
#endif /* not lint */
@ -145,10 +145,12 @@ redirect:
cp = cp->u.c;
goto redirect;
case 'a':
if (appendx >= appendnum)
if (appendx >= appendnum) {
appends = xrealloc(appends,
sizeof(struct s_appends) *
(appendnum *= 2));
(appendnum * 2));
appendnum *= 2;
}
appends[appendx].type = AP_STRING;
appends[appendx].s = cp->t;
appends[appendx].len = strlen(cp->t);
@ -237,10 +239,12 @@ redirect:
flush_appends();
exit(0);
case 'r':
if (appendx >= appendnum)
if (appendx >= appendnum) {
appends = xrealloc(appends,
sizeof(struct s_appends) *
(appendnum *= 2));
(appendnum * 2));
appendnum *= 2;
}
appends[appendx].type = AP_FILE;
appends[appendx].s = cp->t;
appends[appendx].len = strlen(cp->t);
@ -592,8 +596,9 @@ regsub(SPACE *sp, char *string, char *src)
#define NEEDSP(reqlen) \
if (sp->len + (reqlen) + 1 >= sp->blen) { \
sp->blen += (reqlen) + 1024; \
sp->space = sp->back = xrealloc(sp->back, sp->blen); \
size_t newlen = sp->blen + (reqlen) + 1024; \
sp->space = sp->back = xrealloc(sp->back, newlen); \
sp->blen = newlen; \
dst = sp->space + sp->len; \
}
@ -636,8 +641,9 @@ cspace(SPACE *sp, char *p, size_t len, enum e_spflag spflag)
/* Make sure SPACE has enough memory and ramp up quickly. */
tlen = sp->len + len + 1;
if (tlen > sp->blen) {
sp->blen = tlen + 1024;
sp->space = sp->back = xrealloc(sp->back, sp->blen);
size_t newlen = tlen + 1024;
sp->space = sp->back = xrealloc(sp->back, newlen);
sp->blen = newlen;
}
if (spflag == REPLACE)