mirror of
https://github.com/openbsd/src.git
synced 2025-01-10 06:47:55 -08:00
Modify xalloc so that it also takes err(3)-like arguments.
Write an xrealloc wrapper that works the same way, and use it as well. People who feel like it may want to add more explicit error messages to all the places m4 can fail allocating memory... okay tedu@
This commit is contained in:
parent
35999015d1
commit
f6169a2c06
@ -1,4 +1,4 @@
|
||||
/* $OpenBSD: eval.c,v 1.50 2003/06/30 21:47:21 espie Exp $ */
|
||||
/* $OpenBSD: eval.c,v 1.51 2003/11/17 17:12:10 espie Exp $ */
|
||||
/* $NetBSD: eval.c,v 1.7 1996/11/10 21:21:29 pk Exp $ */
|
||||
|
||||
/*
|
||||
@ -37,7 +37,7 @@
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)eval.c 8.2 (Berkeley) 4/27/95";
|
||||
#else
|
||||
static char rcsid[] = "$OpenBSD: eval.c,v 1.50 2003/06/30 21:47:21 espie Exp $";
|
||||
static char rcsid[] = "$OpenBSD: eval.c,v 1.51 2003/11/17 17:12:10 espie Exp $";
|
||||
#endif
|
||||
#endif /* not lint */
|
||||
|
||||
@ -386,7 +386,7 @@ expand_builtin(const char *argv[], int argc, int td)
|
||||
if (argc > 3) {
|
||||
char *temp;
|
||||
|
||||
temp = xalloc(strlen(argv[2])+1);
|
||||
temp = xalloc(strlen(argv[2])+1, NULL);
|
||||
if (argc > 4)
|
||||
map(temp, argv[2], argv[3], argv[4]);
|
||||
else
|
||||
|
@ -1,4 +1,4 @@
|
||||
/* $OpenBSD: extern.h,v 1.38 2003/06/30 22:13:32 espie Exp $ */
|
||||
/* $OpenBSD: extern.h,v 1.39 2003/11/17 17:12:10 espie Exp $ */
|
||||
/* $NetBSD: extern.h,v 1.3 1996/01/13 23:25:24 pk Exp $ */
|
||||
|
||||
/*-
|
||||
@ -99,7 +99,8 @@ extern void pbnum(int);
|
||||
extern void pbunsigned(unsigned long);
|
||||
extern void pbstr(const char *);
|
||||
extern void putback(int);
|
||||
extern void *xalloc(size_t);
|
||||
extern void *xalloc(size_t, const char *fmt, ...);
|
||||
extern void *xrealloc(void *, size_t, const char *fmt, ...);
|
||||
extern char *xstrdup(const char *);
|
||||
extern void usage(void);
|
||||
extern void resizedivs(int);
|
||||
|
@ -1,4 +1,4 @@
|
||||
/* $OpenBSD: gnum4.c,v 1.25 2003/06/30 22:13:32 espie Exp $ */
|
||||
/* $OpenBSD: gnum4.c,v 1.26 2003/11/17 17:12:10 espie Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1999 Marc Espie
|
||||
@ -212,9 +212,7 @@ addchars(const char *c, size_t n)
|
||||
bufsize = 1024;
|
||||
else
|
||||
bufsize *= 2;
|
||||
buffer = realloc(buffer, bufsize);
|
||||
if (buffer == NULL)
|
||||
errx(1, "out of memory");
|
||||
buffer = xrealloc(buffer, bufsize, NULL);
|
||||
}
|
||||
memcpy(buffer+current, c, n);
|
||||
current += n;
|
||||
@ -228,9 +226,7 @@ addchar(int c)
|
||||
bufsize = 1024;
|
||||
else
|
||||
bufsize *= 2;
|
||||
buffer = realloc(buffer, bufsize);
|
||||
if (buffer == NULL)
|
||||
errx(1, "out of memory");
|
||||
buffer = xrealloc(buffer, bufsize, NULL);
|
||||
}
|
||||
buffer[current++] = c;
|
||||
}
|
||||
@ -251,7 +247,8 @@ exit_regerror(int er, regex_t *re)
|
||||
char *errbuf;
|
||||
|
||||
errlen = regerror(er, re, NULL, 0);
|
||||
errbuf = xalloc(errlen);
|
||||
errbuf = xalloc(errlen,
|
||||
"malloc in regerror: %lu", (unsigned long)errlen);
|
||||
regerror(er, re, errbuf, errlen);
|
||||
errx(1, "%s at line %lu: regular expression error: %s",
|
||||
CURRENT_NAME, CURRENT_LINE, errbuf);
|
||||
@ -458,7 +455,7 @@ dopatsubst(const char *argv[], int argc)
|
||||
if (error != 0)
|
||||
exit_regerror(error, &re);
|
||||
|
||||
pmatch = xalloc(sizeof(regmatch_t) * (re.re_nsub+1));
|
||||
pmatch = xalloc(sizeof(regmatch_t) * (re.re_nsub+1), NULL);
|
||||
do_subst(argv[2], &re,
|
||||
argc > 4 && argv[4] != NULL ? argv[4] : "", pmatch);
|
||||
free(pmatch);
|
||||
@ -483,7 +480,7 @@ doregexp(const char *argv[], int argc)
|
||||
if (error != 0)
|
||||
exit_regerror(error, &re);
|
||||
|
||||
pmatch = xalloc(sizeof(regmatch_t) * (re.re_nsub+1));
|
||||
pmatch = xalloc(sizeof(regmatch_t) * (re.re_nsub+1), NULL);
|
||||
if (argv[4] == NULL || argc == 4)
|
||||
do_regexpindex(argv[2], &re, pmatch);
|
||||
else
|
||||
|
@ -1,4 +1,4 @@
|
||||
/* $OpenBSD: look.c,v 1.15 2003/06/30 22:13:32 espie Exp $ */
|
||||
/* $OpenBSD: look.c,v 1.16 2003/11/17 17:12:10 espie Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1989, 1993
|
||||
@ -70,7 +70,7 @@ hash_alloc(s, u)
|
||||
size_t s;
|
||||
void *u UNUSED;
|
||||
{
|
||||
void *storage = xalloc(s);
|
||||
void *storage = xalloc(s, "hash alloc");
|
||||
if (storage)
|
||||
memset(storage, 0, s);
|
||||
return storage;
|
||||
@ -90,7 +90,7 @@ element_alloc(s, u)
|
||||
size_t s;
|
||||
void *u UNUSED;
|
||||
{
|
||||
return xalloc(s);
|
||||
return xalloc(s, "element alloc");
|
||||
}
|
||||
|
||||
void
|
||||
@ -167,7 +167,7 @@ macro_define(const char *name, const char *defn)
|
||||
if (n->d->defn != null)
|
||||
free(n->d->defn);
|
||||
} else {
|
||||
n->d = xalloc(sizeof(struct macro_definition));
|
||||
n->d = xalloc(sizeof(struct macro_definition), NULL);
|
||||
n->d->next = NULL;
|
||||
}
|
||||
setup_definition(n->d, defn, name);
|
||||
@ -180,7 +180,7 @@ macro_pushdef(const char *name, const char *defn)
|
||||
struct macro_definition *d;
|
||||
|
||||
n = create_entry(name);
|
||||
d = xalloc(sizeof(struct macro_definition));
|
||||
d = xalloc(sizeof(struct macro_definition), NULL);
|
||||
d->next = n->d;
|
||||
n->d = d;
|
||||
setup_definition(n->d, defn, name);
|
||||
@ -237,7 +237,7 @@ setup_builtin(const char *name, unsigned int type)
|
||||
|
||||
n = create_entry(name);
|
||||
n->builtin_type = type;
|
||||
n->d = xalloc(sizeof(struct macro_definition));
|
||||
n->d = xalloc(sizeof(struct macro_definition), NULL);
|
||||
n->d->defn = xstrdup(name);
|
||||
n->d->type = type;
|
||||
n->d->next = NULL;
|
||||
|
@ -1,4 +1,4 @@
|
||||
/* $OpenBSD: main.c,v 1.63 2003/06/30 22:13:32 espie Exp $ */
|
||||
/* $OpenBSD: main.c,v 1.64 2003/11/17 17:12:10 espie Exp $ */
|
||||
/* $NetBSD: main.c,v 1.12 1997/02/08 23:54:49 cgd Exp $ */
|
||||
|
||||
/*-
|
||||
@ -43,7 +43,7 @@ static char copyright[] =
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93";
|
||||
#else
|
||||
static char rcsid[] = "$OpenBSD: main.c,v 1.63 2003/06/30 22:13:32 espie Exp $";
|
||||
static char rcsid[] = "$OpenBSD: main.c,v 1.64 2003/11/17 17:12:10 espie Exp $";
|
||||
#endif
|
||||
#endif /* not lint */
|
||||
|
||||
@ -191,8 +191,8 @@ main(int argc, char *argv[])
|
||||
initspaces();
|
||||
STACKMAX = INITSTACKMAX;
|
||||
|
||||
mstack = (stae *)xalloc(sizeof(stae) * STACKMAX);
|
||||
sstack = (char *)xalloc(STACKMAX);
|
||||
mstack = (stae *)xalloc(sizeof(stae) * STACKMAX, NULL);
|
||||
sstack = (char *)xalloc(STACKMAX, NULL);
|
||||
|
||||
maxout = 0;
|
||||
outfile = NULL;
|
||||
@ -623,10 +623,11 @@ dump_stack(struct position *t, int lev)
|
||||
static void
|
||||
enlarge_stack(void)
|
||||
{
|
||||
STACKMAX *= 2;
|
||||
mstack = realloc(mstack, sizeof(stae) * STACKMAX);
|
||||
sstack = realloc(sstack, STACKMAX);
|
||||
if (mstack == NULL || sstack == NULL)
|
||||
errx(1, "Evaluation stack overflow (%lu)",
|
||||
(unsigned long)STACKMAX);
|
||||
STACKMAX += STACKMAX/2;
|
||||
mstack = xrealloc(mstack, sizeof(stae) * STACKMAX,
|
||||
"Evaluation stack overflow (%lu)",
|
||||
(unsigned long)STACKMAX);
|
||||
sstack = xrealloc(sstack, STACKMAX,
|
||||
"Evaluation stack overflow (%lu)",
|
||||
(unsigned long)STACKMAX);
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
/* $OpenBSD: misc.c,v 1.29 2003/06/03 02:56:10 millert Exp $ */
|
||||
/* $OpenBSD: misc.c,v 1.30 2003/11/17 17:12:10 espie Exp $ */
|
||||
/* $NetBSD: misc.c,v 1.6 1995/09/28 05:37:41 tls Exp $ */
|
||||
|
||||
/*
|
||||
@ -37,13 +37,14 @@
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)misc.c 8.1 (Berkeley) 6/6/93";
|
||||
#else
|
||||
static char rcsid[] = "$OpenBSD: misc.c,v 1.29 2003/06/03 02:56:10 millert Exp $";
|
||||
static char rcsid[] = "$OpenBSD: misc.c,v 1.30 2003/11/17 17:12:10 espie Exp $";
|
||||
#endif
|
||||
#endif /* not lint */
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
@ -147,10 +148,10 @@ initspaces()
|
||||
{
|
||||
int i;
|
||||
|
||||
strspace = xalloc(strsize+1);
|
||||
strspace = xalloc(strsize+1, NULL);
|
||||
ep = strspace;
|
||||
endest = strspace+strsize;
|
||||
buf = (char *)xalloc(bufsize);
|
||||
buf = (char *)xalloc(bufsize, NULL);
|
||||
bufbase = buf;
|
||||
bp = buf;
|
||||
endpbb = buf + bufsize;
|
||||
@ -185,10 +186,8 @@ enlarge_bufspace()
|
||||
char *newbuf;
|
||||
int i;
|
||||
|
||||
bufsize *= 2;
|
||||
newbuf = realloc(buf, bufsize);
|
||||
if (!newbuf)
|
||||
errx(1, "too many characters pushed back");
|
||||
bufsize += bufsize/2;
|
||||
newbuf = xrealloc(buf, bufsize, "too many characters pushed back");
|
||||
for (i = 0; i < MAXINP; i++)
|
||||
bbase[i] = (bbase[i]-buf)+newbuf;
|
||||
bp = (bp-buf)+newbuf;
|
||||
@ -254,21 +253,49 @@ resizedivs(int n)
|
||||
{
|
||||
int i;
|
||||
|
||||
outfile = (FILE **)realloc(outfile, sizeof(FILE *) * n);
|
||||
if (outfile == NULL)
|
||||
errx(1, "too many diverts %d", n);
|
||||
outfile = (FILE **)xrealloc(outfile, sizeof(FILE *) * n,
|
||||
"too many diverts %d", n);
|
||||
for (i = maxout; i < n; i++)
|
||||
outfile[i] = NULL;
|
||||
maxout = n;
|
||||
}
|
||||
|
||||
void *
|
||||
xalloc(size_t n)
|
||||
xalloc(size_t n, const char *fmt, ...)
|
||||
{
|
||||
char *p = malloc(n);
|
||||
void *p = malloc(n);
|
||||
|
||||
if (p == NULL)
|
||||
err(1, "malloc");
|
||||
if (p == NULL) {
|
||||
if (fmt == NULL)
|
||||
err(1, "malloc");
|
||||
else {
|
||||
va_list va;
|
||||
|
||||
va_start(va, fmt);
|
||||
verr(1, fmt, va);
|
||||
va_end(va);
|
||||
}
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
void *
|
||||
xrealloc(void *old, size_t n, const char *fmt, ...)
|
||||
{
|
||||
char *p = realloc(old, n);
|
||||
|
||||
if (p == NULL) {
|
||||
free(old);
|
||||
if (fmt == NULL)
|
||||
err(1, "realloc");
|
||||
else {
|
||||
va_list va;
|
||||
|
||||
va_start(va, fmt);
|
||||
verr(1, fmt, va);
|
||||
va_end(va);
|
||||
}
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user