Expand xmalloc in-place, along with xrealloc; which wasn't even ANSI in its

implementation.
This commit is contained in:
obrien 2001-07-24 14:05:21 +00:00
parent 4f82093ece
commit 6dc73139c0
5 changed files with 56 additions and 59 deletions

View File

@ -138,8 +138,10 @@ compile()
*compile_stream(&prog) = NULL;
fixuplabel(prog, NULL);
uselabel();
appends = xmalloc(sizeof(struct s_appends) * appendnum);
match = xmalloc((maxnsub + 1) * sizeof(regmatch_t));
if ((appends = malloc(sizeof(struct s_appends) * appendnum)) == NULL)
err(1, "malloc");
if ((match = malloc((maxnsub + 1) * sizeof(regmatch_t))) == NULL)
err(1, "malloc");
}
#define EATSPACE() do { \
@ -170,7 +172,8 @@ compile_stream(link)
semicolon: EATSPACE();
if (p && (*p == '#' || *p == '\0'))
continue;
*link = cmd = xmalloc(sizeof(struct s_command));
if ((*link = cmd = malloc(sizeof(struct s_command))) == NULL)
err(1, "malloc");
link = &cmd->next;
cmd->nonsel = cmd->inrange = 0;
/* First parse the addresses */
@ -180,14 +183,17 @@ semicolon: EATSPACE();
#define addrchar(c) (strchr("0123456789/\\$", (c)))
if (addrchar(*p)) {
naddr++;
cmd->a1 = xmalloc(sizeof(struct s_addr));
if ((cmd->a1 = malloc(sizeof(struct s_addr))) == NULL)
err(1, "malloc");
p = compile_addr(p, cmd->a1);
EATSPACE(); /* EXTENSION */
if (*p == ',') {
p++;
EATSPACE(); /* EXTENSION */
naddr++;
cmd->a2 = xmalloc(sizeof(struct s_addr));
if ((cmd->a2 = malloc(sizeof(struct s_addr)))
== NULL)
err(1, "malloc");
p = compile_addr(p, cmd->a2);
EATSPACE();
} else
@ -306,7 +312,8 @@ semicolon: EATSPACE();
errx(1,
"%lu: %s: substitute pattern can not be delimited by newline or backslash",
linenum, fname);
cmd->u.s = xmalloc(sizeof(struct s_subst));
if ((cmd->u.s = malloc(sizeof(struct s_subst))) == NULL)
err(1, "malloc");
p = compile_re(p, &cmd->u.s->re);
if (p == NULL)
errx(1,
@ -434,7 +441,8 @@ compile_re(p, repp)
*repp = NULL;
return (p);
}
*repp = xmalloc(sizeof(regex_t));
if ((*repp = malloc(sizeof(regex_t))) == NULL)
err(1, "malloc");
if (p && (eval = regcomp(*repp, re, rflags)) != 0)
errx(1, "%lu: %s: RE error: %s",
linenum, fname, strregerror(eval, *repp));
@ -465,7 +473,8 @@ compile_subst(p, s)
s->maxbref = 0;
s->linenum = linenum;
asize = 2 * _POSIX2_LINE_MAX + 1;
text = xmalloc(asize);
if ((text = malloc(asize)) == NULL)
err(1, "malloc");
size = 0;
do {
op = sp = text + size;
@ -490,7 +499,8 @@ compile_subst(p, s)
}
*sp++ = '\0';
size += sp - op;
s->new = xrealloc(text, size);
if ((s->new = realloc(text, size)) == NULL)
err(1, "realloc");
return (p);
} else if (*p == '\n') {
errx(1,
@ -502,7 +512,8 @@ compile_subst(p, s)
size += sp - op;
if (asize - size < _POSIX2_LINE_MAX + 1) {
asize *= 2;
text = xrealloc(text, asize);
if ((text = realloc(text, asize)) == NULL)
err(1, "realloc");
}
} while (cu_fgets(p = lbuf, sizeof(lbuf), &more));
errx(1, "%lu: %s: unterminated substitute in regular expression",
@ -615,7 +626,8 @@ compile_tr(p, transtab)
errx(1, "%lu: %s: transform strings are not the same length",
linenum, fname);
/* We assume characters are 8 bits */
lt = xmalloc(UCHAR_MAX);
if ((lt = malloc(UCHAR_MAX)) == NULL)
err(1, "malloc");
for (i = 0; i <= UCHAR_MAX; i++)
lt[i] = (char)i;
for (op = old, np = new; *op; op++, np++)
@ -635,7 +647,8 @@ compile_text()
char lbuf[_POSIX2_LINE_MAX + 1];
asize = 2 * _POSIX2_LINE_MAX + 1;
text = xmalloc(asize);
if ((text = malloc(asize)) == NULL)
err(1, "malloc");
size = 0;
while (cu_fgets(lbuf, sizeof(lbuf), NULL)) {
op = s = text + size;
@ -653,11 +666,14 @@ compile_text()
}
if (asize - size < _POSIX2_LINE_MAX + 1) {
asize *= 2;
text = xrealloc(text, asize);
if ((text = realloc(text, asize)) == NULL)
err(1, "realloc");
}
}
text[size] = '\0';
return (xrealloc(text, size + 1));
if ((p = realloc(text, size + 1)) == NULL)
err(1, "realloc");
return (p);
}
/*
@ -708,7 +724,7 @@ duptoeol(s, ctype)
{
size_t len;
int ws;
char *start;
char *p, *start;
ws = 0;
for (start = s; *s != '\0' && *s != '\n'; ++s)
@ -717,7 +733,9 @@ duptoeol(s, ctype)
if (ws)
warnx("%lu: %s: whitespace after %s", linenum, fname, ctype);
len = s - start + 1;
return (memmove(xmalloc(len), start, len));
if ((p = malloc(len)) == NULL)
err(1, "malloc");
return (memmove(p, start, len));
}
/*
@ -773,7 +791,8 @@ enterlabel(cp)
for (lh = *lhp; lh != NULL; lh = lh->lh_next)
if (lh->lh_hash == h && strcmp(cp->t, lh->lh_cmd->t) == 0)
errx(1, "%lu: %s: duplicate label '%s'", linenum, fname, cp->t);
lh = xmalloc(sizeof *lh);
if ((lh = malloc(sizeof *lh)) == NULL)
err(1, "malloc");
lh->lh_next = *lhp;
lh->lh_hash = h;
lh->lh_cmd = cp;

View File

@ -56,5 +56,3 @@ char *cu_fgets __P((char *, int, int *));
int mf_fgets __P((SPACE *, enum e_spflag));
void process __P((void));
char *strregerror __P((int, regex_t *));
void *xmalloc __P((u_int));
void *xrealloc __P((void *, u_int));

View File

@ -130,7 +130,8 @@ main(argc, argv)
break;
case 'e':
eflag = 1;
temp_arg = xmalloc(strlen(optarg) + 2);
if ((temp_arg = malloc(strlen(optarg) + 2)) == NULL)
err(1, "malloc");
strcpy(temp_arg, optarg);
strcat(temp_arg, "\n");
add_compunit(CU_STRING, temp_arg);
@ -362,7 +363,8 @@ add_compunit(type, s)
{
struct s_compunit *cu;
cu = xmalloc(sizeof(struct s_compunit));
if ((cu = malloc(sizeof(struct s_compunit))) == NULL)
err(1, "malloc");
cu->type = type;
cu->s = s;
cu->next = NULL;
@ -379,7 +381,8 @@ add_file(s)
{
struct s_flist *fp;
fp = xmalloc(sizeof(struct s_flist));
if ((fp = malloc(sizeof(struct s_flist))) == NULL)
err(1, "malloc");
fp->next = NULL;
*fl_nextp = fp;
fp->fname = s;

View File

@ -54,36 +54,6 @@ static const char rcsid[] =
#include "defs.h"
#include "extern.h"
/*
* malloc with result test
*/
void *
xmalloc(size)
u_int size;
{
void *p;
if ((p = malloc(size)) == NULL)
err(1, "malloc");
return (p);
}
/*
* realloc with result test
*/
void *
xrealloc(p, size)
void *p;
u_int size;
{
if (p == NULL) /* Compatibility hack. */
return (xmalloc(size));
if ((p = realloc(p, size)) == NULL)
err(1, "realloc");
return (p);
}
/*
* Return a string for a regular expression error passed. This is a overkill,
* because of the silly semantics of regerror (we can never know the size of
@ -100,7 +70,8 @@ strregerror(errcode, preg)
if (oe != NULL)
free(oe);
s = regerror(errcode, preg, "", 0);
oe = xmalloc(s);
if ((oe = malloc(s)) == NULL)
err(1, "malloc");
(void)regerror(errcode, preg, oe, s);
return (oe);
}

View File

@ -113,9 +113,10 @@ process()
goto redirect;
case 'a':
if (appendx >= appendnum)
appends = xrealloc(appends,
if ((appends = realloc(appends,
sizeof(struct s_appends) *
(appendnum *= 2));
(appendnum *= 2))) == NULL)
err(1, "realloc");
appends[appendx].type = AP_STRING;
appends[appendx].s = cp->t;
appends[appendx].len = strlen(cp->t);
@ -201,9 +202,10 @@ process()
exit(0);
case 'r':
if (appendx >= appendnum)
appends = xrealloc(appends,
if ((appends = realloc(appends,
sizeof(struct s_appends) *
(appendnum *= 2));
(appendnum *= 2))) == NULL)
err(1, "realloc");
appends[appendx].type = AP_FILE;
appends[appendx].s = cp->t;
appends[appendx].len = strlen(cp->t);
@ -548,7 +550,9 @@ regsub(sp, string, src)
#define NEEDSP(reqlen) \
if (sp->len >= sp->blen - (reqlen) - 1) { \
sp->blen += (reqlen) + 1024; \
sp->space = sp->back = xrealloc(sp->back, sp->blen); \
if ((sp->space = sp->back = realloc(sp->back, sp->blen)) \
== NULL) \
err(1, "realloc"); \
dst = sp->space + sp->len; \
}
@ -596,7 +600,9 @@ cspace(sp, p, len, spflag)
tlen = sp->len + len + 1;
if (tlen > sp->blen) {
sp->blen = tlen + 1024;
sp->space = sp->back = xrealloc(sp->back, sp->blen);
if ((sp->space = sp->back = realloc(sp->back, sp->blen)) ==
NULL)
err(1, "realloc");
}
if (spflag == REPLACE)