csh: replace malloc(x * y) and realloc(x * y) with reallocarray

This commit is contained in:
nia 2024-04-24 15:49:03 +00:00
parent b544db1689
commit 509d26c5fb
6 changed files with 55 additions and 57 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: dir.c,v 1.35 2020/08/09 00:34:21 dholland Exp $ */
/* $NetBSD: dir.c,v 1.36 2024/04/24 15:49:03 nia Exp $ */
/*-
* Copyright (c) 1980, 1991, 1993
@ -34,7 +34,7 @@
#if 0
static char sccsid[] = "@(#)dir.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: dir.c,v 1.35 2020/08/09 00:34:21 dholland Exp $");
__RCSID("$NetBSD: dir.c,v 1.36 2024/04/24 15:49:03 nia Exp $");
#endif
#endif /* not lint */
@ -279,8 +279,8 @@ dnormalize(Char *cp)
size_t dotdot = 0;
Char *dp, *cwd;
cwd = xmalloc((size_t)((Strlen(dcwd->di_name) + 3) *
sizeof(Char)));
cwd = xreallocarray(NULL, (size_t)(Strlen(dcwd->di_name) + 3),
sizeof(Char));
(void)Strcpy(cwd, dcwd->di_name);
/*
@ -389,7 +389,8 @@ dgoto(Char *cp)
cwdlen = 0;
for (p = cp; *p++;)
continue;
dp = xmalloc((size_t)(cwdlen + (size_t)(p - cp) + 1) * sizeof(Char));
dp = xreallocarray(NULL,
(size_t)(cwdlen + (size_t)(p - cp) + 1), sizeof(Char));
for (p = dp, q = dcwd->di_name; (*p++ = *q++) != '\0';)
continue;
if (cwdlen)
@ -705,8 +706,8 @@ dcanon(Char *cp, Char *p)
/*
* New length is "yyy/" + slink + "/.." and rest
*/
p1 = newcp = xmalloc(
(size_t)((sp - cp) + cc + (p1 - p)) * sizeof(Char));
p1 = newcp = xreallocarray(NULL,
(size_t)((sp - cp) + cc + (p1 - p)), sizeof(Char));
/*
* Copy new path into newcp
*/
@ -725,8 +726,8 @@ dcanon(Char *cp, Char *p)
/*
* New length is slink + "/.." and rest
*/
p1 = newcp = xmalloc(
(size_t)(cc + (p1 - p)) * sizeof(Char));
p1 = newcp = xreallocarray(NULL,
(size_t)(cc + (p1 - p)), sizeof(Char));
/*
* Copy new path into newcp
*/
@ -794,8 +795,8 @@ dcanon(Char *cp, Char *p)
/*
* New length is "yyy/" + slink + "/.." and rest
*/
p1 = newcp = xmalloc(
(size_t)((sp - cp) + cc + (p1 - p)) * sizeof(Char));
p1 = newcp = xreallocarray(NULL,
(size_t)((sp - cp) + cc + (p1 - p)), sizeof(Char));
/*
* Copy new path into newcp
*/
@ -814,8 +815,8 @@ dcanon(Char *cp, Char *p)
/*
* New length is slink + the rest
*/
p1 = newcp = xmalloc(
(size_t)(cc + (p1 - p)) * sizeof(Char));
p1 = newcp = xreallocarray(NULL,
(size_t)(cc + (p1 - p)), sizeof(Char));
/*
* Copy new path into newcp
*/

View File

@ -1,4 +1,4 @@
/* $NetBSD: file.c,v 1.33 2020/09/29 02:58:51 msaitoh Exp $ */
/* $NetBSD: file.c,v 1.34 2024/04/24 15:49:03 nia Exp $ */
/*-
* Copyright (c) 1980, 1991, 1993
@ -34,7 +34,7 @@
#if 0
static char sccsid[] = "@(#)file.c 8.2 (Berkeley) 3/19/94";
#else
__RCSID("$NetBSD: file.c,v 1.33 2020/09/29 02:58:51 msaitoh Exp $");
__RCSID("$NetBSD: file.c,v 1.34 2024/04/24 15:49:03 nia Exp $");
#endif
#endif /* not lint */
@ -519,13 +519,10 @@ again: /* search for matches */
if (command == LIST) {
if ((size_t)numitems >= maxitems) {
maxitems += 1024;
if (items == NULL)
items = xmalloc(sizeof(*items) * maxitems);
else
items = xrealloc(items, sizeof(*items) * maxitems);
items = xreallocarray(items, sizeof(*items), maxitems);
}
items[numitems] = xmalloc((size_t) (Strlen(entry) + 1) *
sizeof(Char));
items[numitems] = xreallocarray(NULL,
(size_t) (Strlen(entry) + 1), sizeof(Char));
copyn(items[numitems], entry, MAXNAMLEN);
numitems++;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: func.c,v 1.44 2020/08/09 00:22:53 dholland Exp $ */
/* $NetBSD: func.c,v 1.45 2024/04/24 15:49:03 nia Exp $ */
/*-
* Copyright (c) 1980, 1991, 1993
@ -34,7 +34,7 @@
#if 0
static char sccsid[] = "@(#)func.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: func.c,v 1.44 2020/08/09 00:22:53 dholland Exp $");
__RCSID("$NetBSD: func.c,v 1.45 2024/04/24 15:49:03 nia Exp $");
#endif
#endif /* not lint */
@ -978,7 +978,7 @@ dounsetenv(Char **v, struct command *t)
maxi = i;
}
name = xmalloc((size_t)(maxi + 1) * sizeof(Char));
name = xreallocarray(NULL, (size_t)(maxi + 1), sizeof(Char));
while (++v && *v)
for (maxi = 1; maxi;)

View File

@ -1,4 +1,4 @@
/* $NetBSD: glob.c,v 1.31 2019/01/05 16:56:25 christos Exp $ */
/* $NetBSD: glob.c,v 1.32 2024/04/24 15:49:03 nia Exp $ */
/*-
* Copyright (c) 1980, 1991, 1993
@ -34,7 +34,7 @@
#if 0
static char sccsid[] = "@(#)glob.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: glob.c,v 1.31 2019/01/05 16:56:25 christos Exp $");
__RCSID("$NetBSD: glob.c,v 1.32 2024/04/24 15:49:03 nia Exp $");
#endif
#endif /* not lint */
@ -131,7 +131,7 @@ globbrace(Char *s, Char *p, Char ***bl)
int i, len, size;
size = GLOBSPACE;
nv = vl = xmalloc(sizeof(Char *) * (size_t)size);
nv = vl = xreallocarray(NULL, sizeof(Char *), (size_t)size);
*vl = NULL;
len = 0;
/* copy part up to the brace */
@ -197,7 +197,7 @@ globbrace(Char *s, Char *p, Char ***bl)
pl = pm + 1;
if (vl == &nv[size]) {
size += GLOBSPACE;
nv = xrealloc(nv, (size_t)size * sizeof(Char *));
nv = xreallocarray(nv, (size_t)size, sizeof(Char *));
vl = &nv[size - GLOBSPACE];
}
}
@ -250,7 +250,7 @@ expbrace(Char ***nvp, Char ***elp, size_t size)
size += (size_t)(GLOBSPACE > l ? GLOBSPACE : l);
l = vl - nv;
e = ex - nv;
nv = xrealloc(nv, (size_t)size * sizeof(Char *));
nv = xreallocarray(nv, size, sizeof(Char *));
vl = nv + l;
ex = nv + e;
}
@ -279,7 +279,7 @@ globexpand(Char **v)
size_t size;
size = GLOBSPACE;
nv = vl = xmalloc(sizeof(Char *) * size);
nv = vl = xreallocarray(NULL, sizeof(Char *), size);
*vl = NULL;
/*
@ -294,7 +294,7 @@ globexpand(Char **v)
*vl++ = pargv[i];
if (vl == &nv[size]) {
size += GLOBSPACE;
nv = xrealloc(nv, (size_t)size * sizeof(Char *));
nv = xreallocarray(nv, size, sizeof(Char *));
vl = &nv[size - GLOBSPACE];
}
}
@ -305,7 +305,7 @@ globexpand(Char **v)
*vl++ = Strsave(s);
if (vl == &nv[size]) {
size += GLOBSPACE;
nv = xrealloc(nv, size * sizeof(Char *));
nv = xreallocarray(nv, size, sizeof(Char *));
vl = &nv[size - GLOBSPACE];
}
}
@ -512,7 +512,7 @@ void
ginit(void)
{
gargsiz = GLOBSPACE;
gargv = xmalloc(sizeof(Char *) * (size_t)gargsiz);
gargv = xreallocarray(NULL, sizeof(Char *), (size_t)gargsiz);
gargv[0] = 0;
gargc = 0;
}
@ -591,7 +591,7 @@ dobackp(Char *cp, int literal)
blkfree(pargv);
}
pargsiz = GLOBSPACE;
pargv = xmalloc(sizeof(Char *) * (size_t)pargsiz);
pargv = xreallocarray(NULL, sizeof(Char *), (size_t)pargsiz);
pargv[0] = NULL;
pargcp = pargs = word;
pargc = 0;
@ -777,7 +777,7 @@ pword(void)
psave(0);
if (pargc == pargsiz - 1) {
pargsiz += GLOBSPACE;
pargv = xrealloc(pargv, (size_t)pargsiz * sizeof(Char *));
pargv = xreallocarray(pargv, (size_t)pargsiz, sizeof(Char *));
}
pargv[pargc++] = Strsave(pargs);
pargv[pargc] = NULL;
@ -799,7 +799,7 @@ Gmatch(Char *string, Char *pattern)
pattern++;
}
blk = xmalloc(GLOBSPACE * sizeof(Char *));
blk = xreallocarray(NULL, GLOBSPACE, sizeof(Char *));
blk[0] = Strsave(pattern);
blk[1] = NULL;
@ -897,10 +897,10 @@ Gcat(Char *s1, Char *s2)
n = (p - s1) + (q - s2) - 1;
if (++gargc >= gargsiz) {
gargsiz += GLOBSPACE;
gargv = xrealloc(gargv, (size_t)gargsiz * sizeof(Char *));
gargv = xreallocarray(gargv, (size_t)gargsiz, sizeof(Char *));
}
gargv[gargc] = 0;
p = gargv[gargc - 1] = xmalloc((size_t)n * sizeof(Char));
p = gargv[gargc - 1] = xreallocarray(NULL, (size_t)n, sizeof(Char));
for (q = s1; (*p++ = *q++) != '\0';)
continue;
for (p--, q = s2; (*p++ = *q++) != '\0';)

View File

@ -1,4 +1,4 @@
/* $NetBSD: misc.c,v 1.22 2019/01/05 16:54:00 christos Exp $ */
/* $NetBSD: misc.c,v 1.23 2024/04/24 15:49:03 nia Exp $ */
/*-
* Copyright (c) 1980, 1991, 1993
@ -34,7 +34,7 @@
#if 0
static char sccsid[] = "@(#)misc.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: misc.c,v 1.22 2019/01/05 16:54:00 christos Exp $");
__RCSID("$NetBSD: misc.c,v 1.23 2024/04/24 15:49:03 nia Exp $");
#endif
#endif /* not lint */
@ -72,7 +72,7 @@ strsave(const char *s)
s = "";
for (n = s; *n++;)
continue;
r = p = xmalloc((size_t)(n - s) * sizeof(*p));
r = p = xreallocarray(NULL, (size_t)(n - s), sizeof(*p));
while ((*p++ = *s++) != '\0')
continue;
return (r);
@ -189,7 +189,7 @@ strspl(char *cp, char *dp)
continue;
for (q = dp; *q++;)
continue;
ep = xmalloc((size_t)(((p - cp) + (q - dp) - 1) * sizeof(*ep)));
ep = xreallocarray(NULL, (size_t)(((p - cp) + (q - dp) - 1), sizeof(*ep)));
for (p = ep, q = cp; *p++ = *q++;)
continue;
for (p--, q = dp; *p++ = *q++;)

View File

@ -1,4 +1,4 @@
/* $NetBSD: str.c,v 1.16 2019/01/05 16:54:00 christos Exp $ */
/* $NetBSD: str.c,v 1.17 2024/04/24 15:49:03 nia Exp $ */
/*-
* Copyright (c) 1991, 1993
@ -34,7 +34,7 @@
#if 0
static char sccsid[] = "@(#)str.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: str.c,v 1.16 2019/01/05 16:54:00 christos Exp $");
__RCSID("$NetBSD: str.c,v 1.17 2024/04/24 15:49:03 nia Exp $");
#endif
#endif /* not lint */
@ -66,7 +66,7 @@ blk2short(char **src)
*/
for (n = 0; src[n] != NULL; n++)
continue;
sdst = dst = xmalloc((size_t)((n + 1) * sizeof(*dst)));
sdst = dst = xreallocarray(NULL, n + 1, sizeof(*dst));
for (; *src != NULL; src++)
*dst++ = SAVE(*src);
@ -85,7 +85,7 @@ short2blk(Char *const *src)
*/
for (n = 0; src[n] != NULL; n++)
continue;
sdst = dst = xmalloc((size_t)((n + 1) * sizeof(*dst)));
sdst = dst = xreallocarray(NULL, n + 1, sizeof(*dst));
for (; *src != NULL; src++)
*dst++ = strsave(short2str(*src));
@ -105,7 +105,7 @@ str2short(const char *src)
if (sdst == (NULL)) {
dstsize = MALLOC_INCR;
sdst = xmalloc((size_t)dstsize * sizeof(*sdst));
sdst = xreallocarray(NULL, (size_t)dstsize, sizeof(*sdst));
}
dst = sdst;
@ -114,7 +114,7 @@ str2short(const char *src)
*dst++ = (Char) ((unsigned char) *src++);
if (dst == edst) {
dstsize += MALLOC_INCR;
sdst = xrealloc(sdst, (size_t)dstsize * sizeof(*sdst));
sdst = xreallocarray(sdst, (size_t)dstsize, sizeof(*sdst));
edst = &sdst[dstsize];
dst = &edst[-MALLOC_INCR];
}
@ -135,7 +135,7 @@ short2str(const Char *src)
if (sdst == NULL) {
dstsize = MALLOC_INCR;
sdst = xmalloc((size_t)dstsize * sizeof(*sdst));
sdst = xreallocarray(NULL, dstsize, sizeof(*sdst));
}
dst = sdst;
edst = &dst[dstsize];
@ -143,7 +143,7 @@ short2str(const Char *src)
*dst++ = (char) *src++;
if (dst == edst) {
dstsize += MALLOC_INCR;
sdst = xrealloc(sdst, (size_t)dstsize * sizeof(*sdst));
sdst = xreallocarray(sdst, dstsize, sizeof(*sdst));
edst = &sdst[dstsize];
dst = &edst[-MALLOC_INCR];
}
@ -312,7 +312,7 @@ s_strsave(const Char *s)
s = STRNULL;
for (p = s; *p++;)
continue;
p = n = xmalloc((size_t)(p - s) * sizeof(*n));
p = n = xreallocarray(NULL, (size_t)(p - s), sizeof(*n));
while ((*n++ = *s++) != '\0')
continue;
return __UNCONST(p);
@ -332,7 +332,7 @@ s_strspl(const Char *cp, const Char *dp)
continue;
for (q = dp; *q++;)
continue;
ep = xmalloc((size_t)((p - cp) + (q - dp) - 1) * sizeof(*ep));
ep = xreallocarray(NULL, (size_t)((p - cp) + (q - dp) - 1), sizeof(*ep));
for (d = ep, q = cp; (*d++ = *q++) != '\0';)
continue;
for (d--, q = dp; (*d++ = *q++) != '\0';)
@ -378,7 +378,7 @@ short2qstr(const Char *src)
if (sdst == NULL) {
dstsize = MALLOC_INCR;
sdst = xmalloc((size_t)dstsize * sizeof(*sdst));
sdst = xreallocarray(NULL, dstsize, sizeof(*sdst));
}
dst = sdst;
edst = &dst[dstsize];
@ -388,7 +388,7 @@ short2qstr(const Char *src)
*dst++ = '\\';
if (dst == edst) {
dstsize += MALLOC_INCR;
sdst = xrealloc(sdst, (size_t)dstsize * sizeof(*sdst));
sdst = xreallocarray(sdst, (size_t)dstsize, sizeof(*sdst));
edst = &sdst[dstsize];
dst = &edst[-MALLOC_INCR];
}
@ -396,7 +396,7 @@ short2qstr(const Char *src)
*dst++ = (char) *src++;
if (dst == edst) {
dstsize += MALLOC_INCR;
sdst = xrealloc(sdst, (size_t)dstsize * sizeof(*sdst));
sdst = xreallocarray(sdst, (size_t)dstsize, sizeof(*sdst));
edst = &sdst[dstsize];
dst = &edst[-MALLOC_INCR];
}
@ -424,8 +424,8 @@ vis_str(const Char *cp)
n = ((size_t)(dp - cp) << 2) + 1; /* 4 times + NULL */
if (dstsize < n) {
sdst = (dstsize ?
xrealloc(sdst, (size_t)n * sizeof(*sdst)) :
xmalloc((size_t)n * sizeof(*sdst)));
xreallocarray(sdst, (size_t)n, sizeof(*sdst)) :
xreallocarray(NULL, (size_t)n, sizeof(*sdst)));
dstsize = n;
}
/*