Expand x{malloc,calloc,realloc,strdup} in-place.

(even found some unchecked naked uses)
This commit is contained in:
David E. O'Brien 2001-07-24 14:02:07 +00:00
parent 0e9ea6e71e
commit 3c5bf66cec
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=80284
8 changed files with 169 additions and 79 deletions

View File

@ -920,7 +920,8 @@ pointer:
asterisk:
T_MULT {
$$ = xcalloc(1, sizeof (pqinf_t));
if (($$ = calloc(1, sizeof (pqinf_t))) == NULL)
nomem();
$$->p_pcnt = 1;
}
;
@ -936,7 +937,8 @@ type_qualifier_list:
type_qualifier:
T_QUAL {
$$ = xcalloc(1, sizeof (pqinf_t));
if (($$ = calloc(1, sizeof (pqinf_t))) == NULL)
nomem();
if ($1 == CONST) {
$$->p_const = 1;
} else {

View File

@ -163,7 +163,8 @@ initdecl()
};
/* declaration stack */
dcs = xcalloc(1, sizeof (dinfo_t));
if ((dcs = calloc(1, sizeof (dinfo_t))) == NULL)
nomem();
dcs->d_ctx = EXTERN;
dcs->d_ldlsym = &dcs->d_dlsyms;
@ -176,7 +177,8 @@ initdecl()
}
/* shared type structures */
typetab = xcalloc(NTSPEC, sizeof (type_t));
if ((typetab = calloc(NTSPEC, sizeof (type_t))) == NULL)
nomem();
for (i = 0; i < NTSPEC; i++)
typetab[i].t_tspec = NOTSPEC;
typetab[CHAR].t_tspec = CHAR;
@ -557,7 +559,8 @@ pushdecl(sc)
(void)printf("pushdecl(%d)\n", (int)sc);
/* put a new element on the declaration stack */
di = xcalloc(1, sizeof (dinfo_t));
if ((di = calloc(1, sizeof (dinfo_t))) == NULL)
nomem();
di->d_nxt = dcs;
dcs = di;
di->d_ctx = sc;

View File

@ -144,7 +144,8 @@ pushctrl(env)
{
cstk_t *ci;
ci = xcalloc(1, sizeof (cstk_t));
if ((ci = calloc(1, sizeof (cstk_t))) == NULL)
nomem();
ci->c_env = env;
ci->c_nxt = cstk;
cstk = ci;
@ -463,7 +464,8 @@ label(typ, sym, tn)
* to the type of the switch expression
*/
v = constant(tn);
nv = xcalloc(1, sizeof (val_t));
if ((nv = calloc(1, sizeof (val_t))) == NULL)
nomem();
cvtcon(CASE, 0, ci->c_swtype, nv, v);
free(v);
@ -483,7 +485,8 @@ label(typ, sym, tn)
* append the value to the list of
* case values
*/
cl = xcalloc(1, sizeof (clst_t));
if ((cl = calloc(1, sizeof (clst_t))) == NULL)
nomem();
STRUCT_ASSIGN(cl->cl_val, *nv);
cl->cl_nxt = ci->c_clst;
ci->c_clst = cl;
@ -591,7 +594,8 @@ switch1(tn)
* duplicated. This is not too complicated because it is
* only an integer type.
*/
tp = xcalloc(1, sizeof (type_t));
if ((tp = calloc(1, sizeof (type_t))) == NULL)
nomem();
if (tn != NULL) {
tp->t_tspec = tn->tn_type->t_tspec;
if ((tp->t_isenum = tn->tn_type->t_isenum) != 0)

View File

@ -100,9 +100,11 @@ fnnalloc(s, len)
return (NULL);
if ((fn = srchfn(s, len)) == NULL) {
fn = xmalloc(sizeof (fn_t));
if ((fn = malloc(sizeof (fn_t))) == NULL)
nomem();
/* Do not used strdup() because string is not NUL-terminated.*/
fn->fn_name = xmalloc(len + 1);
if ((fn->fn_name = malloc(len + 1)) == NULL)
nomem();
(void)memcpy(fn->fn_name, s, len);
fn->fn_name[len] = '\0';
fn->fn_len = len;
@ -174,7 +176,8 @@ xnewblk()
mbl_t *mb;
int prot, flags;
mb = xmalloc(sizeof (mbl_t));
if ((mb = malloc(sizeof (mbl_t))) == NULL)
nomem();
/* use mmap instead of malloc to avoid malloc's size overhead */
@ -252,7 +255,8 @@ initmem()
pgsz = getpagesize();
mblklen = ((MBLKSIZ + pgsz - 1) / pgsz) * pgsz;
mblks = xcalloc(nmblks = ML_INC, sizeof (mbl_t *));
if ((mblks = calloc(nmblks = ML_INC, sizeof (mbl_t *))) == NULL)
nomem();
}
@ -265,7 +269,9 @@ getlblk(l, s)
size_t s;
{
while (l >= nmblks) {
mblks = xrealloc(mblks, (nmblks + ML_INC) * sizeof (mbl_t *));
if ((mblks = realloc(mblks, (nmblks + ML_INC) *
sizeof (mbl_t *))) == NULL)
nomem();
(void)memset(&mblks[nmblks], 0, ML_INC * sizeof (mbl_t *));
nmblks += ML_INC;
}

View File

@ -306,7 +306,8 @@ allocsb()
if ((sb = sbfrlst) != NULL) {
sbfrlst = sb->sb_nxt;
} else {
sb = xmalloc(sizeof (sbuf_t));
if ((sb = malloc(sizeof (sbuf_t))) == NULL)
nomem();
}
(void)memset(sb, 0, sizeof (sb));
return (sb);
@ -565,7 +566,9 @@ icon(base)
uq = (u_quad_t)xsign((quad_t)uq, typ, -1);
(yylval.y_val = xcalloc(1, sizeof (val_t)))->v_tspec = typ;
if ((yylval.y_val = calloc(1, sizeof(val_t))) == NULL)
nomem();
yylval.y_val->v_tspec = typ;
yylval.y_val->v_ansiu = ansiu;
yylval.y_val->v_quad = (quad_t)uq;
@ -672,7 +675,9 @@ fcon()
}
}
(yylval.y_val = xcalloc(1, sizeof (val_t)))->v_tspec = typ;
if ((yylval.y_val = calloc(1, sizeof (val_t))) == NULL)
nomem();
yylval.y_val->v_tspec = typ;
if (typ == FLOAT) {
yylval.y_val->v_ldbl = f;
} else {
@ -726,7 +731,8 @@ ccon()
val = cv;
}
yylval.y_val = xcalloc(1, sizeof (val_t));
if ((yylval.y_val = calloc(1, sizeof (val_t))) == NULL)
nomem();
yylval.y_val->v_tspec = INT;
yylval.y_val->v_quad = val;
@ -772,7 +778,8 @@ wccon()
}
}
yylval.y_val = xcalloc(1, sizeof (val_t));
if ((yylval.y_val = calloc(1, sizeof (val_t))) == NULL)
nomem();
yylval.y_val->v_tspec = WCHAR;
yylval.y_val->v_quad = wc;
@ -1125,13 +1132,15 @@ string()
size_t len, max;
strg_t *strg;
s = xmalloc(max = 64);
if ((s = malloc(max = 64)) == NULL)
nomem();
len = 0;
while ((c = getescc('"')) >= 0) {
/* +1 to reserve space for a trailing NUL character */
if (len + 1 == max)
s = xrealloc(s, max *= 2);
if ((s = realloc(s, max *= 2)) == NULL)
nomem();
s[len++] = (char)c;
}
s[len] = '\0';
@ -1139,7 +1148,8 @@ string()
/* unterminated string constant */
error(258);
strg = xcalloc(1, sizeof (strg_t));
if ((strg = calloc(1, sizeof (strg_t))) == NULL)
nomem();
strg->st_tspec = CHAR;
strg->st_len = len;
strg->st_cp = s;
@ -1157,12 +1167,14 @@ wcstrg()
wchar_t *ws;
strg_t *strg;
s = xmalloc(max = 64);
if ((s = malloc(max = 64)) == NULL)
nomem();
len = 0;
while ((c = getescc('"')) >= 0) {
/* +1 to save space for a trailing NUL character */
if (len + 1 >= max)
s = xrealloc(s, max *= 2);
if ((s = realloc(s, max *= 2)) == NULL)
nomem();
s[len++] = (char)c;
}
s[len] = '\0';
@ -1182,7 +1194,8 @@ wcstrg()
n = 1;
}
ws = xmalloc((wlen + 1) * sizeof (wchar_t));
if ((ws = malloc((wlen + 1) * sizeof (wchar_t))) == NULL)
nomem();
/* convert from multibyte to wide char */
(void)mbtowc(NULL, NULL, 0);
@ -1195,7 +1208,8 @@ wcstrg()
ws[wi] = 0;
free(s);
strg = xcalloc(1, sizeof (strg_t));
if ((strg = calloc(1, sizeof (strg_t))) == NULL)
nomem();
strg->st_tspec = WCHAR;
strg->st_len = wlen;
strg->st_wcp = ws;

View File

@ -2694,7 +2694,8 @@ fold(tn)
u_quad_t ul, ur;
tnode_t *cn;
v = xcalloc(1, sizeof (val_t));
if ((v = calloc(1, sizeof (val_t))) == NULL)
nomem();
v->v_tspec = t = tn->tn_type->t_tspec;
utyp = t == PTR || isutyp(t);
@ -2840,7 +2841,8 @@ foldtst(tn)
int l, r;
val_t *v;
v = xcalloc(1, sizeof (val_t));
if ((v = calloc(1, sizeof (val_t))) == NULL)
nomem();
v->v_tspec = tn->tn_type->t_tspec;
if (tn->tn_type->t_tspec != INT)
lerror("foldtst() 1");
@ -2898,7 +2900,8 @@ foldflt(tn)
tspec_t t;
ldbl_t l, r;
v = xcalloc(1, sizeof (val_t));
if ((v = calloc(1, sizeof (val_t))) == NULL)
nomem();
v->v_tspec = t = tn->tn_type->t_tspec;
if (!isftyp(t))
@ -3265,7 +3268,8 @@ parg(n, tp, tn)
tnode_t *ln;
int warn;
ln = xcalloc(1, sizeof (tnode_t));
if ((ln = calloc(1, sizeof (tnode_t))) == NULL)
nomem();
ln->tn_type = tduptyp(tp);
ln->tn_type->t_const = 0;
ln->tn_lvalue = 1;
@ -3293,7 +3297,8 @@ constant(tn)
if (tn != NULL)
tn = promote(NOOP, 0, tn);
v = xcalloc(1, sizeof (val_t));
if ((v = calloc(1, sizeof (val_t))) == NULL)
nomem();
if (tn == NULL) {
if (nerr == 0)
@ -3458,7 +3463,8 @@ displexpr(tn, offs)
char *s;
size_t n;
n = MB_CUR_MAX * (tn->tn_strg->st_len + 1);
s = xmalloc(n);
if ((s = malloc(n)) == NULL)
nomem();
(void)wcstombs(s, tn->tn_strg->st_wcp, n);
(void)printf("L\"%s\"", s);
free(s);
@ -3829,12 +3835,14 @@ catstrg(strg1, strg2)
len = (len1 = strg1->st_len) + (len2 = strg2->st_len);
if (strg1->st_tspec == CHAR) {
strg1->st_cp = xrealloc(strg1->st_cp, len + 1);
if ((strg1->st_cp = realloc(strg1->st_cp, len + 1)) == NULL)
nomem();
(void)memcpy(strg1->st_cp + len1, strg2->st_cp, len2 + 1);
free(strg2->st_cp);
} else {
strg1->st_wcp = xrealloc(strg1->st_wcp,
(len + 1) * sizeof (wchar_t));
if ((strg1->st_wcp = realloc(strg1->st_wcp, (len + 1) *
sizeof (wchar_t))) == NULL)
nomem();
(void)memcpy(strg1->st_wcp + len1, strg2->st_wcp,
(len2 + 1) * sizeof (wchar_t));
free(strg2->st_wcp);

View File

@ -110,13 +110,17 @@ readfile(name)
pos_t pos;
if (inpfns == NULL)
inpfns = xcalloc(ninpfns = 128, sizeof (short));
if ((inpfns = calloc(ninpfns = 128, sizeof (short))) == NULL)
nomem();
if (fnames == NULL)
fnames = xcalloc(nfnames = 256, sizeof (char *));
if ((fnames = calloc(nfnames = 256, sizeof (char *))) == NULL)
nomem();
if (tlstlen == 0)
tlst = xcalloc(tlstlen = 256, sizeof (type_t *));
if ((tlst = calloc(tlstlen = 256, sizeof (type_t *))) == NULL)
nomem();
if (thtab == NULL)
thtab = xcalloc(THSHSIZ2, sizeof (thtab_t));
if ((thtab = calloc(THSHSIZ2, sizeof (thtab_t))) == NULL)
nomem();
srcfile = getfnidx(name);
@ -233,7 +237,9 @@ setfnid(fid, cp)
inperr();
if (fid >= ninpfns) {
inpfns = xrealloc(inpfns, (ninpfns * 2) * sizeof (short));
if ((inpfns = realloc(inpfns, (ninpfns * 2) * sizeof (short)))
== NULL)
nomem();
(void)memset(inpfns + ninpfns, 0, ninpfns * sizeof (short));
ninpfns *= 2;
}
@ -581,8 +587,9 @@ inptype(cp, epp)
tp->t_proto = 1;
narg = (int)strtol(cp, &eptr, 10);
cp = eptr;
tp->t_args = xcalloc((size_t)(narg + 1),
sizeof (type_t *));
if ((tp->t_args = calloc((size_t)(narg + 1),
sizeof (type_t *))) == NULL)
nomem();
for (i = 0; i < narg; i++) {
if (i == narg - 1 && *cp == 'E') {
tp->t_vararg = 1;
@ -832,7 +839,9 @@ storetyp(tp, cp, len, h)
errx(1, "sorry, too many types");
if (tidx == tlstlen - 1) {
tlst = xrealloc(tlst, (tlstlen * 2) * sizeof (type_t *));
if ((tlst = realloc(tlst, (tlstlen * 2) * sizeof (type_t *)))
== NULL)
nomem();
(void)memset(tlst + tlstlen, 0, tlstlen * sizeof (type_t *));
tlstlen *= 2;
}
@ -883,7 +892,8 @@ inpqstrg(src, epp)
int c;
int v;
dst = strg = xmalloc(slen = 32);
if ((dst = strg = malloc(slen = 32)) == NULL)
nomem();
if ((c = *src++) != '"')
inperr();
@ -949,7 +959,8 @@ inpqstrg(src, epp)
}
/* keep space for trailing '\0' */
if (dst - strg == slen - 1) {
strg = xrealloc(strg, slen * 2);
if ((strg = realloc(strg, slen * 2)) == NULL)
nomem();
dst = strg + (slen - 1);
slen *= 2;
}
@ -980,7 +991,8 @@ inpname(cp, epp)
inperr();
cp = eptr;
if (len + 1 > blen)
buf = xrealloc(buf, blen = len + 1);
if ((buf = realloc(buf, blen = len + 1)) == NULL)
nomem();
for (i = 0; i < len; i++) {
c = *cp++;
if (!isalnum(c) && c != '_')
@ -1012,12 +1024,15 @@ getfnidx(fn)
return (i);
if (i == nfnames - 1) {
fnames = xrealloc(fnames, (nfnames * 2) * sizeof (char *));
if ((fnames = realloc(fnames, (nfnames * 2) * sizeof (char *)))
== NULL)
nomem();
(void)memset(fnames + nfnames, 0, nfnames * sizeof (char *));
nfnames *= 2;
}
fnames[i] = xstrdup(fn);
if ((fnames[i] = strdup(fn)) == NULL)
nomem();
return (i);
}

View File

@ -145,7 +145,8 @@ appstrg(lstp, s)
olst = *lstp;
for (i = 0; olst[i] != NULL; i++) ;
lst = xmalloc((i + 2) * sizeof (char *));
if ((lst = malloc((i + 2) * sizeof (char *))) == NULL)
nomem();
(void)memcpy(lst, olst, i * sizeof (char *));
lst[i] = s;
lst[i + 1] = NULL;
@ -157,7 +158,11 @@ appcstrg(lstp, s)
char ***lstp;
const char *s;
{
appstrg(lstp, xstrdup(s));
char *p;
if ((p = strdup(s)) == NULL)
nomem();
appstrg(lstp, p);
}
static void
@ -171,10 +176,12 @@ applst(destp, src)
odest = *destp;
for (i = 0; odest[i] != NULL; i++) ;
for (k = 0; src[k] != NULL; k++) ;
dest = xmalloc((i + k + 1) * sizeof (char *));
if ((dest = malloc((i + k + 1) * sizeof (char *))) == NULL)
nomem();
(void)memcpy(dest, odest, i * sizeof (char *));
for (k = 0; src[k] != NULL; k++)
dest[i + k] = xstrdup(src[k]);
if ((dest[i + k] = strdup(src[k])) == NULL)
nomem();
dest[i + k] = NULL;
*destp = dest;
free(odest);
@ -201,7 +208,8 @@ concat2(s1, s2)
{
char *s;
s = xmalloc(strlen(s1) + strlen(s2) + 1);
if ((s = malloc(strlen(s1) + strlen(s2) + 1)) == NULL)
nomem();
(void)strcpy(s, s1);
(void)strcat(s, s2);
@ -214,7 +222,8 @@ concat3(s1, s2, s3)
{
char *s;
s = xmalloc(strlen(s1) + strlen(s2) + strlen(s3) + 1);
if ((s = malloc(strlen(s1) + strlen(s2) + strlen(s3) + 1)) == NULL)
nomem();
(void)strcpy(s, s1);
(void)strcat(s, s2);
(void)strcat(s, s3);
@ -301,14 +310,17 @@ main(argc, argv)
size_t len;
if ((tmp = getenv("TMPDIR")) == NULL || (len = strlen(tmp)) == 0) {
tmpdir = xstrdup(_PATH_TMP);
if ((tmpdir = strdup(_PATH_TMP)) == NULL)
nomem();
} else {
s = xmalloc(len + 2);
if ((s = malloc(len + 2)) == NULL)
nomem();
(void)sprintf(s, "%s%s", tmp, tmp[len - 1] == '/' ? "" : "/");
tmpdir = s;
}
cppout = xmalloc(strlen(tmpdir) + sizeof ("lint0.XXXXXX"));
if ((cppout = malloc(strlen(tmpdir) + sizeof ("lint0.XXXXXX"))) == NULL)
nomem();
(void)sprintf(cppout, "%slint0.XXXXXX", tmpdir);
cppoutfd = mkstemp(cppout);
if (cppoutfd == -1) {
@ -316,16 +328,26 @@ main(argc, argv)
terminate(-1);
}
p1out = xcalloc(1, sizeof (char *));
p2in = xcalloc(1, sizeof (char *));
cflags = xcalloc(1, sizeof (char *));
lcflags = xcalloc(1, sizeof (char *));
l1flags = xcalloc(1, sizeof (char *));
l2flags = xcalloc(1, sizeof (char *));
l2libs = xcalloc(1, sizeof (char *));
deflibs = xcalloc(1, sizeof (char *));
libs = xcalloc(1, sizeof (char *));
libsrchpath = xcalloc(1, sizeof (char *));
if ((p1out = calloc(1, sizeof (char *))) == NULL)
nomem();
if ((p2in = calloc(1, sizeof (char *))) == NULL)
nomem();
if ((cflags = calloc(1, sizeof (char *))) == NULL)
nomem();
if ((lcflags = calloc(1, sizeof (char *))) == NULL)
nomem();
if ((l1flags = calloc(1, sizeof (char *))) == NULL)
nomem();
if ((l2flags = calloc(1, sizeof (char *))) == NULL)
nomem();
if ((l2libs = calloc(1, sizeof (char *))) == NULL)
nomem();
if ((deflibs = calloc(1, sizeof (char *))) == NULL)
nomem();
if ((libs = calloc(1, sizeof (char *))) == NULL)
nomem();
if ((libsrchpath = calloc(1, sizeof (char *))) == NULL)
nomem();
appcstrg(&cflags, "-E");
appcstrg(&cflags, "-x");
@ -435,7 +457,9 @@ main(argc, argv)
usage();
Cflag = 1;
appstrg(&l2flags, concat2("-C", optarg));
p2out = xmalloc(sizeof ("llib-l.ln") + strlen(optarg));
if ((p2out = malloc(sizeof ("llib-l.ln") +
strlen(optarg))) == NULL)
nomem();
(void)sprintf(p2out, "llib-l%s.ln", optarg);
freelst(&deflibs);
break;
@ -455,7 +479,8 @@ main(argc, argv)
if (Cflag || oflag)
usage();
oflag = 1;
outputfn = xstrdup(optarg);
if ((outputfn = strdup(optarg)) == NULL)
nomem();
break;
case 'L':
@ -550,12 +575,15 @@ fname(name, last)
outputfn = NULL;
oflag = 0;
} else if (iflag) {
ofn = xmalloc(strlen(bn) + (bn == suff ? 4 : 2));
if ((ofn = malloc(strlen(bn) + (bn == suff ? 4 : 2))) == NULL)
nomem();
len = bn == suff ? strlen(bn) : (suff - 1) - bn;
(void)sprintf(ofn, "%.*s", (int)len, bn);
(void)strcat(ofn, ".ln");
} else {
ofn = xmalloc(strlen(tmpdir) + sizeof ("lint1.XXXXXX"));
if ((ofn = malloc(strlen(tmpdir) + sizeof ("lint1.XXXXXX"))) ==
NULL)
nomem();
(void)sprintf(ofn, "%slint1.XXXXXX", tmpdir);
fd = mkstemp(ofn);
if (fd == -1) {
@ -567,11 +595,13 @@ fname(name, last)
if (!iflag)
appcstrg(&p1out, ofn);
args = xcalloc(1, sizeof (char *));
if ((args = calloc(1, sizeof (char *))) == NULL)
nomem();
/* run cc */
path = xmalloc(strlen(PATH_USRBIN) + sizeof ("/cc"));
if ((path = malloc(strlen(PATH_USRBIN) + sizeof ("/cc"))) == NULL)
nomem();
(void)sprintf(path, "%s/cc", PATH_USRBIN);
appcstrg(&args, path);
@ -595,7 +625,8 @@ fname(name, last)
/* run lint1 */
path = xmalloc(strlen(PATH_LIBEXEC) + sizeof ("/lint1"));
if ((path = malloc(strlen(PATH_LIBEXEC) + sizeof ("/lint1"))) == NULL)
nomem();
(void)sprintf(path, "%s/lint1", PATH_LIBEXEC);
appcstrg(&args, path);
@ -682,11 +713,15 @@ findlibs(liblst)
for (i = 0; (lib = liblst[i]) != NULL; i++) {
for (k = 0; (path = libsrchpath[k]) != NULL; k++) {
len = strlen(path) + strlen(lib);
lfn = xrealloc(lfn, len + sizeof ("/llib-l.ln"));
if ((lfn = realloc(lfn, len + sizeof ("/llib-l.ln")))
== NULL)
nomem();
(void)sprintf(lfn, "%s/llib-l%s.ln", path, lib);
if (rdok(lfn))
break;
lfn = xrealloc(lfn, len + sizeof ("/lint/llib-l.ln"));
if ((lfn = realloc(lfn, len +
sizeof("/lint/llib-l.ln"))) == NULL)
nomem();
(void)sprintf(lfn, "%s/lint/llib-l%s.ln", path, lib);
if (rdok(lfn))
break;
@ -721,9 +756,11 @@ lint2()
{
char *path, **args;
args = xcalloc(1, sizeof (char *));
if ((args = calloc(1, sizeof (char *))) == NULL)
nomem();
path = xmalloc(strlen(PATH_LIBEXEC) + sizeof ("/lint2"));
if ((path = malloc(strlen(PATH_LIBEXEC) + sizeof ("/lint2"))) == NULL)
nomem();
(void)sprintf(path, "%s/lint2", PATH_LIBEXEC);
appcstrg(&args, path);
@ -751,7 +788,8 @@ cat(srcs, dest)
terminate(-1);
}
buf = xmalloc(MBLKSIZ);
if ((buf = malloc(MBLKSIZ)) == NULL)
nomem();
for (i = 0; (src = srcs[i]) != NULL; i++) {
if ((ifd = open(src, O_RDONLY)) == -1) {