commit 3e2aa6163d87ed572c10828708e343e2acfcff0f
Author: FRIGN <dev_AT_frign.de>
Date: Wed Mar 11 10:50:18 2015 +0100
Remove mallocarray(...) and use reallocarray(NULL, ...)
After a short correspondence with Otto Moerbeek it turned out
mallocarray() is only in the OpenBSD-Kernel, because the kernel-
malloc doesn't have realloc.
Userspace applications should rather use reallocarray with an
explicit NULL-pointer.
Assuming reallocarray() will become available in c-stdlibs in the
next few years, we nip mallocarray() in the bud to allow an easy
transition to a system-provided version when the day comes.
diff --git a/Makefile b/Makefile
index 7ca299b..5c55f43 100644
--- a/Makefile
+++ b/Makefile
_AT_@ -54,7 +54,6 @@ LIBUTILSRC =\
libutil/fnck.c\
libutil/getlines.c\
libutil/human.c\
- libutil/mallocarray.c\
libutil/md5.c\
libutil/mode.c\
libutil/putword.c\
diff --git a/col.c b/col.c
index e6aa1c3..8b86240 100644
--- a/col.c
+++ b/col.c
_AT_@ -177,7 +177,7 @@ allocbuf(void)
{
char **bp;
- buff = emallocarray(pagsize, sizeof(*buff));
+ buff = ereallocarray(NULL, pagsize, sizeof(*buff));
for (bp = buff; bp < &buff[pagsize]; ++bp)
*bp = emalloc(NCOLS);
}
diff --git a/cut.c b/cut.c
index 586636c..d916cf4 100644
--- a/cut.c
+++ b/cut.c
_AT_@ -56,7 +56,7 @@ parselist(char *str)
if (*s == ',')
n++;
}
- r = emallocarray(n, sizeof(*r));
+ r = ereallocarray(NULL, n, sizeof(*r));
for (s = str; n; n--, s++) {
r->min = (*s == '-') ? 1 : strtoul(s, &s, 10);
r->max = (*s == '-') ? strtoul(s + 1, &s, 10) : r->min;
diff --git a/find.c b/find.c
index 91827c6..9b876dc 100644
--- a/find.c
+++ b/find.c
_AT_@ -595,7 +595,7 @@ get_exec_arg(char *argv[], Extra *extra)
e->u.p.arglen = e->u.p.filelen = 0;
e->u.p.first = e->u.p.next = arg - argv - 1;
e->u.p.cap = (arg - argv) * 2;
- e->argv = emallocarray(e->u.p.cap, sizeof(*e->argv));
+ e->argv = ereallocarray(NULL, e->u.p.cap, sizeof(*e->argv));
for (arg = argv, new = e->argv; *arg; arg++, new++) {
*new = *arg;
_AT_@ -604,7 +604,7 @@ get_exec_arg(char *argv[], Extra *extra)
arg++; /* due to our extra NULL */
} else {
e->argv = argv;
- e->u.s.braces = emallocarray(++nbraces, sizeof(*e->u.s.braces)); /* ++ for NULL */
+ e->u.s.braces = ereallocarray(NULL, ++nbraces, sizeof(*e->u.s.braces)); /* ++ for NULL */
for (arg = argv, braces = e->u.s.braces; *arg; arg++)
if (!strcmp(*arg, "{}"))
_AT_@ -632,7 +632,7 @@ get_ok_arg(char *argv[], Extra *extra)
*arg = NULL;
o->argv = argv;
- o->braces = emallocarray(++nbraces, sizeof(*o->braces));
+ o->braces = ereallocarray(NULL, ++nbraces, sizeof(*o->braces));
for (arg = argv, braces = o->braces; *arg; arg++)
if (!strcmp(*arg, "{}"))
_AT_@ -824,7 +824,7 @@ parse(int argc, char **argv)
*
https://en.wikipedia.org/wiki/Shunting-yard_algorithm
* read from infix, resulting rpn ends up in rpn, next position in rpn is out
* push operators onto stack, next position in stack is top */
- rpn = emallocarray(ntok + gflags.print, sizeof(*rpn));
+ rpn = ereallocarray(NULL, ntok + gflags.print, sizeof(*rpn));
for (tok = infix, out = rpn, top = stack; tok->type != END; tok++) {
switch (tok->type) {
case PRIM: *out++ = *tok; break;
diff --git a/libutil/mallocarray.c b/libutil/mallocarray.c
deleted file mode 100644
index e89dfd2..0000000
--- a/libutil/mallocarray.c
+++ /dev/null
_AT_@ -1,50 +0,0 @@
-/*
- * Copyright (c) 2008 Otto Moerbeek <otto_AT_drijf.net>
- *
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- */
-
-#include <sys/types.h>
-#include <errno.h>
-#include <stdint.h>
-#include <stdlib.h>
-
-#include "../util.h"
-
-/*
- * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
- * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
- */
-#define MUL_NO_OVERFLOW (1UL << (sizeof(size_t) * 4))
-
-void *
-mallocarray(size_t nmemb, size_t size)
-{
- if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
- nmemb > 0 && SIZE_MAX / nmemb < size) {
- errno = ENOMEM;
- return NULL;
- }
- return malloc(nmemb * size);
-}
-
-void *
-emallocarray(size_t nmemb, size_t size)
-{
- void *p;
-
- if (!(p = mallocarray(nmemb, size)))
- eprintf("mallocarray: out of memory\n");
-
- return p;
-}
diff --git a/ls.c b/ls.c
index 0099f5b..6a36ff4 100644
--- a/ls.c
+++ b/ls.c
_AT_@ -341,7 +341,7 @@ main(int argc, char *argv[])
if (argc == 0)
*--argv = ".", argc++;
- ents = emallocarray(argc, sizeof(*ents));
+ ents = ereallocarray(NULL, argc, sizeof(*ents));
for (i = 0; i < argc; i++)
mkent(&ents[i], argv[i], 1, Hflag || Lflag);
diff --git a/paste.c b/paste.c
index 99046cc..a3b4078 100644
--- a/paste.c
+++ b/paste.c
_AT_@ -107,11 +107,11 @@ main(int argc, char *argv[])
/* populate delimiters */
unescape(adelim);
- delim = emallocarray(utflen(adelim) + 1, sizeof(*delim));
+ delim = ereallocarray(NULL, utflen(adelim) + 1, sizeof(*delim));
len = utftorunestr(adelim, delim);
/* populate file list */
- dsc = emallocarray(argc, sizeof(*dsc));
+ dsc = ereallocarray(NULL, argc, sizeof(*dsc));
for (i = 0; i < argc; i++) {
if (strcmp(argv[i], "-") == 0)
diff --git a/printf.c b/printf.c
index 656b808..4517d36 100644
--- a/printf.c
+++ b/printf.c
_AT_@ -111,7 +111,7 @@ main(int argc, char *argv[])
break;
case 'c':
unescape(arg);
- rarg = emallocarray(utflen(arg) + 1, sizeof(*rarg));
+ rarg = ereallocarray(NULL, utflen(arg) + 1, sizeof(*rarg));
utftorunestr(arg, rarg);
efputrune(rarg, stdout, "<stdout>");
free(rarg);
_AT_@ -125,7 +125,7 @@ main(int argc, char *argv[])
if (arg[j] == '\'' || arg[j] == '\"') {
arg += j + 1;
unescape(arg);
- rarg = emallocarray(utflen(arg) + 1, sizeof(*rarg));
+ rarg = ereallocarray(NULL, utflen(arg) + 1, sizeof(*rarg));
utftorunestr(arg, rarg);
num = rarg[0];
} else
diff --git a/sed.c b/sed.c
index d413c71..54282cf 100644
--- a/sed.c
+++ b/sed.c
_AT_@ -598,7 +598,7 @@ strtorunes(char *s, size_t nrunes)
{
Rune *rs, *rp;
- rp = rs = emallocarray(nrunes + 1, sizeof(*rs));
+ rp = rs = ereallocarray(NULL, nrunes + 1, sizeof(*rs));
while (nrunes--)
s += chartorune(rp++, s);
diff --git a/strings.c b/strings.c
index b1872eb..4a7f685 100644
--- a/strings.c
+++ b/strings.c
_AT_@ -15,7 +15,7 @@ strings(FILE *fp, const char *fname, size_t len)
size_t i, bread;
off_t off;
- rbuf = emallocarray(len, sizeof(*rbuf));
+ rbuf = ereallocarray(NULL, len, sizeof(*rbuf));
for (off = 0, i = 0; (bread = efgetrune(&r, fp, fname)); ) {
off += bread;
diff --git a/tr.c b/tr.c
index 039df36..6ca1488 100644
--- a/tr.c
+++ b/tr.c
_AT_@ -78,9 +78,9 @@ makeset(char *str, struct range **set, int (**check)(Rune))
/* rstr defines at most len ranges */
unescape(str);
- rstr = emallocarray(utflen(str) + 1, sizeof(*rstr));
+ rstr = ereallocarray(NULL, utflen(str) + 1, sizeof(*rstr));
len = utftorunestr(str, rstr);
- *set = emallocarray(len, sizeof(**set));
+ *set = ereallocarray(NULL, len, sizeof(**set));
for (i = 0; i < len; i++) {
if (rstr[i] == '[') {
diff --git a/util.h b/util.h
index c4e7585..7370272 100644
--- a/util.h
+++ b/util.h
_AT_@ -25,8 +25,6 @@ void apathmax(char **, size_t *);
void *ecalloc(size_t, size_t);
void *emalloc(size_t);
-void *mallocarray(size_t, size_t);
-void *emallocarray(size_t, size_t);
void *erealloc(void *, size_t);
void *reallocarray(void *, size_t, size_t);
void *ereallocarray(void *, size_t, size_t);
Received on Wed Mar 11 2015 - 13:42:34 CET