summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorderaadt <deraadt@openbsd.org>2014-10-16 19:43:31 +0000
committerderaadt <deraadt@openbsd.org>2014-10-16 19:43:31 +0000
commita8627d2c40d5ed23e746aca6d869ba89c5df59e7 (patch)
tree9eef843c1894b7a53cd101ef566a6ed1733f6d5d
parentAdditonal registers needs to be saved in sc_save2, not sc_save. (diff)
downloadwireguard-openbsd-a8627d2c40d5ed23e746aca6d869ba89c5df59e7.tar.xz
wireguard-openbsd-a8627d2c40d5ed23e746aca6d869ba89c5df59e7.zip
Use reallocarray() and remove a few archaic memory allocation practices.
ok deraadt
-rw-r--r--bin/csh/alloc.c14
-rw-r--r--bin/csh/csh.c4
-rw-r--r--bin/csh/csh.h3
-rw-r--r--bin/csh/dir.c25
-rw-r--r--bin/csh/dol.c4
-rw-r--r--bin/csh/extern.h3
-rw-r--r--bin/csh/file.c11
-rw-r--r--bin/csh/func.c4
-rw-r--r--bin/csh/glob.c32
-rw-r--r--bin/csh/misc.c6
-rw-r--r--bin/csh/set.c4
-rw-r--r--bin/csh/str.c33
12 files changed, 68 insertions, 75 deletions
diff --git a/bin/csh/alloc.c b/bin/csh/alloc.c
index 8b16a2ab51e..3683e2fc514 100644
--- a/bin/csh/alloc.c
+++ b/bin/csh/alloc.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: alloc.c,v 1.11 2014/10/16 18:23:26 deraadt Exp $ */
+/* $OpenBSD: alloc.c,v 1.12 2014/10/16 19:43:31 deraadt Exp $ */
/* $NetBSD: alloc.c,v 1.6 1995/03/21 09:02:23 cgd Exp $ */
/*-
@@ -63,6 +63,18 @@ Realloc(ptr_t p, size_t n)
}
ptr_t
+Reallocarray(ptr_t p, size_t c, size_t n)
+{
+ ptr_t ptr;
+
+ if ((ptr = reallocarray(p, c, n)) == (ptr_t) 0) {
+ child++;
+ stderror(ERR_NOMEM);
+ }
+ return (ptr);
+}
+
+ptr_t
Calloc(size_t s, size_t n)
{
ptr_t ptr;
diff --git a/bin/csh/csh.c b/bin/csh/csh.c
index 9c9d7dac2be..fb908d3f9d8 100644
--- a/bin/csh/csh.c
+++ b/bin/csh/csh.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: csh.c,v 1.25 2009/10/27 23:59:21 deraadt Exp $ */
+/* $OpenBSD: csh.c,v 1.26 2014/10/16 19:43:31 deraadt Exp $ */
/* $NetBSD: csh.c,v 1.14 1995/04/29 23:21:28 mycroft Exp $ */
/*-
@@ -1288,7 +1288,7 @@ defaultpath(void)
Char **blk, **blkp;
struct stat stb;
- blkp = blk = (Char **) xmalloc((size_t) sizeof(Char *) * 10);
+ blkp = blk = xreallocarray(NULL, 10, sizeof(Char *));
#define DIRAPPEND(a) \
if (stat(ptr = a, &stb) == 0 && S_ISDIR(stb.st_mode)) \
diff --git a/bin/csh/csh.h b/bin/csh/csh.h
index 492784939a1..e700778e9f3 100644
--- a/bin/csh/csh.h
+++ b/bin/csh/csh.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: csh.h,v 1.17 2005/03/13 19:08:27 cloder Exp $ */
+/* $OpenBSD: csh.h,v 1.18 2014/10/16 19:43:31 deraadt Exp $ */
/* $NetBSD: csh.h,v 1.9 1995/03/21 09:02:40 cgd Exp $ */
/*-
@@ -86,6 +86,7 @@ typedef void *ptr_t;
#define xmalloc(i) Malloc(i)
#define xrealloc(p, i) Realloc(p, i)
+#define xreallocarray(p, i, j) Reallocarray(p, i, j)
#define xcalloc(n, s) Calloc(n, s)
#define xfree(p) Free(p)
diff --git a/bin/csh/dir.c b/bin/csh/dir.c
index 2d196f4554b..a4b81eb512c 100644
--- a/bin/csh/dir.c
+++ b/bin/csh/dir.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: dir.c,v 1.16 2014/07/12 02:47:51 guenther Exp $ */
+/* $OpenBSD: dir.c,v 1.17 2014/10/16 19:43:31 deraadt Exp $ */
/* $NetBSD: dir.c,v 1.9 1995/03/21 09:02:42 cgd Exp $ */
/*-
@@ -132,7 +132,7 @@ dset(Char *dp)
* Don't call set() directly cause if the directory contains ` or
* other junk characters glob will fail.
*/
- Char **vec = (Char **) xmalloc((size_t) (2 * sizeof(Char **)));
+ Char **vec = xreallocarray(NULL, 2, sizeof(Char **));
vec[0] = Strsave(dp);
vec[1] = 0;
@@ -267,7 +267,7 @@ dnormalize(Char *cp)
size_t len;
len = (size_t) (Strlen(dcwd->di_name) + 3);
- cwd = (Char *) xmalloc(len * sizeof(Char));
+ cwd = xreallocarray(NULL, len, sizeof(Char));
(void) Strlcpy(cwd, dcwd->di_name, len);
/*
@@ -377,7 +377,7 @@ dgoto(Char *cp)
cwdlen = 0;
for (p = cp; *p++;)
continue;
- dp = (Char *) xmalloc((size_t)((cwdlen + (p - cp) + 1) * sizeof(Char)));
+ dp = xreallocarray(NULL, (cwdlen + (p - cp) + 1), sizeof(Char));
for (p = dp, q = dcwd->di_name; (*p++ = *q++) != '\0';)
continue;
if (cwdlen)
@@ -701,9 +701,8 @@ dcanon(Char *cp, Char *p)
/*
* New length is "yyy/" + link + "/.." and rest
*/
- p1 = newcp = (Char *) xmalloc((size_t)
- (((sp - cp) + cc + (p1 - p)) *
- sizeof(Char)));
+ p1 = newcp = xreallocarray(NULL,
+ (sp - cp) + cc + (p1 - p), sizeof(Char));
/*
* Copy new path into newcp
*/
@@ -722,8 +721,8 @@ dcanon(Char *cp, Char *p)
/*
* New length is link + "/.." and rest
*/
- p1 = newcp = (Char *) xmalloc((size_t)
- ((cc + (p1 - p)) * sizeof(Char)));
+ p1 = newcp = xreallocarray(NULL, cc + (p1 - p),
+ sizeof(Char));
/*
* Copy new path into newcp
*/
@@ -792,9 +791,8 @@ dcanon(Char *cp, Char *p)
/*
* New length is "yyy/" + link + "/.." and rest
*/
- p1 = newcp = (Char *) xmalloc((size_t)
- (((sp - cp) + cc + (p1 - p))
- * sizeof(Char)));
+ p1 = newcp = xreallocarray(NULL,
+ (sp - cp) + cc + (p1 - p), sizeof(Char));
/*
* Copy new path into newcp
*/
@@ -813,8 +811,7 @@ dcanon(Char *cp, Char *p)
/*
* New length is link + the rest
*/
- p1 = newcp = (Char *) xmalloc((size_t)
- ((cc + (p1 - p)) * sizeof(Char)));
+ p1 = newcp = xreallocarray(NULL, cc + (p1 - p), sizeof(Char));
/*
* Copy new path into newcp
*/
diff --git a/bin/csh/dol.c b/bin/csh/dol.c
index 864a7906657..a53c2dc764c 100644
--- a/bin/csh/dol.c
+++ b/bin/csh/dol.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: dol.c,v 1.17 2010/08/12 02:00:27 kevlo Exp $ */
+/* $OpenBSD: dol.c,v 1.18 2014/10/16 19:43:31 deraadt Exp $ */
/* $NetBSD: dol.c,v 1.8 1995/09/27 00:38:38 jtc Exp $ */
/*-
@@ -712,7 +712,7 @@ setDolp(Char *cp)
if (dp) {
size_t len = Strlen(cp) + 1 - lhlen + rhlen;
- np = (Char *) xmalloc(len * sizeof(Char));
+ np = xreallocarray(NULL, len, sizeof(Char));
*dp = 0;
(void) Strlcpy(np, cp, len);
(void) Strlcat(np, rhsub, len);
diff --git a/bin/csh/extern.h b/bin/csh/extern.h
index 45b214ae23c..cc382a6ace3 100644
--- a/bin/csh/extern.h
+++ b/bin/csh/extern.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: extern.h,v 1.17 2014/10/16 18:23:26 deraadt Exp $ */
+/* $OpenBSD: extern.h,v 1.18 2014/10/16 19:43:31 deraadt Exp $ */
/* $NetBSD: extern.h,v 1.8 1996/10/31 23:50:54 christos Exp $ */
/*-
@@ -297,6 +297,7 @@ void psecs(long);
void Free(ptr_t);
ptr_t Malloc(size_t);
ptr_t Realloc(ptr_t, size_t);
+ptr_t Reallocarray(ptr_t, size_t, size_t);
ptr_t Calloc(size_t, size_t);
/*
diff --git a/bin/csh/file.c b/bin/csh/file.c
index c8fa07395ac..11961e6e37a 100644
--- a/bin/csh/file.c
+++ b/bin/csh/file.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: file.c,v 1.16 2009/10/27 23:59:21 deraadt Exp $ */
+/* $OpenBSD: file.c,v 1.17 2014/10/16 19:43:31 deraadt Exp $ */
/* $NetBSD: file.c,v 1.11 1996/11/08 19:34:37 christos Exp $ */
/*-
@@ -450,14 +450,9 @@ again: /* search for matches */
if (command == LIST) {
if (numitems >= maxitems) {
maxitems += 1024;
- if (items == NULL)
- items = (Char **) xmalloc(sizeof(*items) * maxitems);
- else
- items = (Char **) xrealloc((ptr_t) items,
- sizeof(*items) * maxitems);
+ items = xreallocarray(items, maxitems, sizeof(*items));
}
- items[numitems] = (Char *) xmalloc((size_t) (Strlen(entry) + 1) *
- sizeof(Char));
+ items[numitems] = xreallocarray(NULL, (Strlen(entry) + 1), sizeof(Char));
copyn(items[numitems], entry, MAXNAMLEN);
numitems++;
}
diff --git a/bin/csh/func.c b/bin/csh/func.c
index 786fd778d5e..ca02f726af7 100644
--- a/bin/csh/func.c
+++ b/bin/csh/func.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: func.c,v 1.25 2009/10/27 23:59:21 deraadt Exp $ */
+/* $OpenBSD: func.c,v 1.26 2014/10/16 19:43:31 deraadt Exp $ */
/* $NetBSD: func.c,v 1.11 1996/02/09 02:28:29 christos Exp $ */
/*-
@@ -954,7 +954,7 @@ dounsetenv(Char **v, struct command *t)
maxi = i;
}
- name = (Char *) xmalloc((size_t) (maxi + 1) * sizeof(Char));
+ name = xreallocarray(NULL, maxi + 1, sizeof(Char));
while (++v && *v)
for (maxi = 1; maxi;)
diff --git a/bin/csh/glob.c b/bin/csh/glob.c
index ecdff678306..75605e57e64 100644
--- a/bin/csh/glob.c
+++ b/bin/csh/glob.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: glob.c,v 1.13 2009/10/27 23:59:21 deraadt Exp $ */
+/* $OpenBSD: glob.c,v 1.14 2014/10/16 19:43:31 deraadt Exp $ */
/* $NetBSD: glob.c,v 1.10 1995/03/21 09:03:01 cgd Exp $ */
/*-
@@ -122,7 +122,7 @@ globbrace(Char *s, Char *p, Char ***bl)
Char gbuf[MAXPATHLEN];
int size = GLOBSPACE;
- nv = vl = (Char **) xmalloc((size_t) sizeof(Char *) * size);
+ nv = vl = xreallocarray(NULL, size, sizeof(Char *));
*vl = NULL;
len = 0;
@@ -189,8 +189,7 @@ globbrace(Char *s, Char *p, Char ***bl)
pl = pm + 1;
if (vl == &nv[size]) {
size += GLOBSPACE;
- nv = (Char **) xrealloc((ptr_t) nv, (size_t)
- size * sizeof(Char *));
+ nv = xreallocarray(nv, size, sizeof(Char *));
vl = &nv[size - GLOBSPACE];
}
}
@@ -245,8 +244,7 @@ expbrace(Char ***nvp, Char ***elp, int size)
size += GLOBSPACE > l ? GLOBSPACE : l;
l = vl - nv;
e = el - nv;
- nv = (Char **) xrealloc((ptr_t) nv, (size_t)
- size * sizeof(Char *));
+ nv = xreallocarray(nv, size, sizeof(Char *));
vl = nv + l;
el = nv + e;
}
@@ -276,7 +274,7 @@ globexpand(Char **v)
int size = GLOBSPACE;
- nv = vl = (Char **) xmalloc((size_t) sizeof(Char *) * size);
+ nv = vl = xreallocarray(NULL, size, sizeof(Char *));
*vl = NULL;
/*
@@ -291,8 +289,7 @@ globexpand(Char **v)
*vl++ = pargv[i];
if (vl == &nv[size]) {
size += GLOBSPACE;
- nv = (Char **) xrealloc((ptr_t) nv,
- (size_t) size * sizeof(Char *));
+ nv = xreallocarray(nv, size, sizeof(Char *));
vl = &nv[size - GLOBSPACE];
}
}
@@ -303,8 +300,7 @@ globexpand(Char **v)
*vl++ = Strsave(s);
if (vl == &nv[size]) {
size += GLOBSPACE;
- nv = (Char **) xrealloc((ptr_t) nv, (size_t)
- size * sizeof(Char *));
+ nv = xreallocarray(nv, size, sizeof(Char *));
vl = &nv[size - GLOBSPACE];
}
}
@@ -508,7 +504,7 @@ void
ginit(void)
{
gargsiz = GLOBSPACE;
- gargv = (Char **) xmalloc((size_t) sizeof(Char *) * gargsiz);
+ gargv = xreallocarray(NULL, gargsiz, sizeof(Char *));
gargv[0] = 0;
gargc = 0;
}
@@ -588,7 +584,7 @@ dobackp(Char *cp, bool literal)
blkfree(pargv);
}
pargsiz = GLOBSPACE;
- pargv = (Char **) xmalloc((size_t) sizeof(Char *) * pargsiz);
+ pargv = xreallocarray(NULL, pargsiz, sizeof(Char *));
pargv[0] = NULL;
pargcp = pargs = word;
pargc = 0;
@@ -768,8 +764,7 @@ pword(void)
psave(0);
if (pargc == pargsiz - 1) {
pargsiz += GLOBSPACE;
- pargv = (Char **) xrealloc((ptr_t) pargv,
- (size_t) pargsiz * sizeof(Char *));
+ pargv = xreallocarray(pargv, pargsiz, sizeof(Char *));
}
pargv[pargc++] = Strsave(pargs);
pargv[pargc] = NULL;
@@ -788,7 +783,7 @@ Gmatch(Char *string, Char *pattern)
pattern++;
}
- blk = (Char **) xmalloc(GLOBSPACE * sizeof(Char *));
+ blk = xreallocarray(NULL, GLOBSPACE, sizeof(Char *));
blk[0] = Strsave(pattern);
blk[1] = NULL;
@@ -869,11 +864,10 @@ Gcat(Char *s1, Char *s2)
n = (p - s1) + (q - s2) - 1;
if (++gargc >= gargsiz) {
gargsiz += GLOBSPACE;
- gargv = (Char **) xrealloc((ptr_t) gargv,
- (size_t) gargsiz * sizeof(Char *));
+ gargv = xreallocarray(gargv, gargsiz, sizeof(Char *));
}
gargv[gargc] = 0;
- p = gargv[gargc - 1] = (Char *) xmalloc((size_t) n * sizeof(Char));
+ p = gargv[gargc - 1] = (Char *) xreallocarray(NULL, n, sizeof(Char));
for (q = s1; (*p++ = *q++) != '\0';)
continue;
for (p--, q = s2; (*p++ = *q++) != '\0';)
diff --git a/bin/csh/misc.c b/bin/csh/misc.c
index 8fb3d8f0327..89b1e016a0a 100644
--- a/bin/csh/misc.c
+++ b/bin/csh/misc.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: misc.c,v 1.12 2012/12/04 02:24:46 deraadt Exp $ */
+/* $OpenBSD: misc.c,v 1.13 2014/10/16 19:43:31 deraadt Exp $ */
/* $NetBSD: misc.c,v 1.6 1995/03/21 09:03:09 cgd Exp $ */
/*-
@@ -61,7 +61,7 @@ strsave(char *s)
s = "";
for (p = s; *p++;)
continue;
- n = p = (char *) xmalloc((size_t) ((p - s) * sizeof(char)));
+ n = p = xreallocarray(NULL, (p - s), sizeof(char));
while ((*p++ = *s++) != '\0')
continue;
return (n);
@@ -173,7 +173,7 @@ strspl(char *cp, char *dp)
continue;
for (q = dp; *q++;)
continue;
- ep = (char *) xmalloc((size_t) (((p - cp) + (q - dp) - 1) * sizeof(char)));
+ ep = xreallocarray(NULL, ((p - cp) + (q - dp) - 1), sizeof(char));
for (p = ep, q = cp; *p++ = *q++;)
continue;
for (p--, q = dp; *p++ = *q++;)
diff --git a/bin/csh/set.c b/bin/csh/set.c
index b8b862239f5..0edd885c653 100644
--- a/bin/csh/set.c
+++ b/bin/csh/set.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: set.c,v 1.12 2009/10/28 02:03:47 schwarze Exp $ */
+/* $OpenBSD: set.c,v 1.13 2014/10/16 19:43:31 deraadt Exp $ */
/* $NetBSD: set.c,v 1.8 1995/03/21 18:35:52 mycroft Exp $ */
/*-
@@ -437,7 +437,7 @@ adrof1(Char *name, struct varent *v)
void
set(Char *var, Char *val)
{
- Char **vec = (Char **) xmalloc((size_t) (2 * sizeof(Char **)));
+ Char **vec = xreallocarray(NULL, 2, sizeof(Char **));
vec[0] = val;
vec[1] = 0;
diff --git a/bin/csh/str.c b/bin/csh/str.c
index 44b48064ea6..f5d908c910e 100644
--- a/bin/csh/str.c
+++ b/bin/csh/str.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: str.c,v 1.15 2009/10/27 23:59:21 deraadt Exp $ */
+/* $OpenBSD: str.c,v 1.16 2014/10/16 19:43:31 deraadt Exp $ */
/* $NetBSD: str.c,v 1.6 1995/03/21 09:03:24 cgd Exp $ */
/*-
@@ -57,7 +57,7 @@ blk2short(char **src)
*/
for (n = 0; src[n] != NULL; n++)
continue;
- sdst = dst = (Char **) xmalloc((size_t) ((n + 1) * sizeof(Char *)));
+ sdst = dst = xreallocarray(NULL, n + 1, sizeof(Char *));
for (; *src != NULL; src++)
*dst++ = SAVE(*src);
@@ -76,7 +76,7 @@ short2blk(Char **src)
*/
for (n = 0; src[n] != NULL; n++)
continue;
- sdst = dst = (char **) xmalloc((size_t) ((n + 1) * sizeof(char *)));
+ sdst = dst = xreallocarray(NULL, n + 1, sizeof(char *));
for (; *src != NULL; src++)
*dst++ = strsave(short2str(*src));
@@ -96,7 +96,7 @@ str2short(char *src)
if (sdst == (NULL)) {
dstsize = MALLOC_INCR;
- sdst = (Char *) xmalloc((size_t) dstsize * sizeof(Char));
+ sdst = xreallocarray(NULL, dstsize, sizeof(Char));
}
dst = sdst;
@@ -105,8 +105,7 @@ str2short(char *src)
*dst++ = (Char) ((unsigned char) *src++);
if (dst == edst) {
dstsize += MALLOC_INCR;
- sdst = (Char *) xrealloc((ptr_t) sdst,
- (size_t) dstsize * sizeof(Char));
+ sdst = xreallocarray(sdst, dstsize, sizeof(Char));
edst = &sdst[dstsize];
dst = &edst[-MALLOC_INCR];
}
@@ -127,7 +126,7 @@ short2str(Char *src)
if (sdst == NULL) {
dstsize = MALLOC_INCR;
- sdst = (char *) xmalloc((size_t) dstsize * sizeof(char));
+ sdst = xreallocarray(NULL, dstsize, sizeof(char));
}
dst = sdst;
edst = &dst[dstsize];
@@ -135,8 +134,7 @@ short2str(Char *src)
*dst++ = (char) *src++;
if (dst == edst) {
dstsize += MALLOC_INCR;
- sdst = (char *) xrealloc((ptr_t) sdst,
- (size_t) dstsize * sizeof(char));
+ sdst = xreallocarray(sdst, dstsize, sizeof(char));
edst = &sdst[dstsize];
dst = &edst[-MALLOC_INCR];
}
@@ -291,7 +289,7 @@ s_strsave(Char *s)
s = STRNULL;
for (p = s; *p++;)
continue;
- n = p = (Char *) xmalloc((size_t) ((p - s) * sizeof(Char)));
+ n = p = xreallocarray(NULL, p - s, sizeof(Char));
while ((*p++ = *s++) != '\0')
continue;
return (n);
@@ -311,8 +309,7 @@ s_strspl(Char *cp, Char *dp)
continue;
for (q = dp; *q++;)
continue;
- ep = (Char *) xmalloc((size_t)
- (((p - cp) + (q - dp) - 1) * sizeof(Char)));
+ ep = xreallocarray(NULL, ((p - cp) + (q - dp) - 1), sizeof(Char));
for (p = ep, q = cp; (*p++ = *q++) != '\0';)
continue;
for (p--, q = dp; (*p++ = *q++) != '\0';)
@@ -358,7 +355,7 @@ short2qstr(Char *src)
if (sdst == NULL) {
dstsize = MALLOC_INCR;
- sdst = (char *) xmalloc((size_t) dstsize * sizeof(char));
+ sdst = xreallocarray(NULL, dstsize, sizeof(char));
}
dst = sdst;
edst = &dst[dstsize];
@@ -367,8 +364,7 @@ short2qstr(Char *src)
*dst++ = '\\';
if (dst == edst) {
dstsize += MALLOC_INCR;
- sdst = (char *) xrealloc((ptr_t) sdst,
- (size_t) dstsize * sizeof(char));
+ sdst = xreallocarray(sdst, dstsize, sizeof(char));
edst = &sdst[dstsize];
dst = &edst[-MALLOC_INCR];
}
@@ -376,8 +372,7 @@ short2qstr(Char *src)
*dst++ = (char) *src++;
if (dst == edst) {
dstsize += MALLOC_INCR;
- sdst = (char *) xrealloc((ptr_t) sdst,
- (size_t) dstsize * sizeof(char));
+ sdst = xreallocarray(sdst, dstsize, sizeof(char));
edst = &sdst[dstsize];
dst = &edst[-MALLOC_INCR];
}
@@ -404,9 +399,7 @@ vis_str(Char *cp)
continue;
n = ((dp - cp) << 2) + 1; /* 4 times + NUL */
if (dstsize < n) {
- sdst = (char *) (dstsize ?
- xrealloc(sdst, (size_t) n * sizeof(char)) :
- xmalloc((size_t) n * sizeof(char)));
+ sdst = xreallocarray(sdst, n, sizeof(char));
dstsize = n;
}
/*