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.
This commit is contained in:
FRIGN 2015-03-11 10:50:18 +01:00
parent d6818a3c5f
commit 833c2aebb4
12 changed files with 15 additions and 68 deletions

View File

@ -54,7 +54,6 @@ LIBUTILSRC =\
libutil/fnck.c\ libutil/fnck.c\
libutil/getlines.c\ libutil/getlines.c\
libutil/human.c\ libutil/human.c\
libutil/mallocarray.c\
libutil/md5.c\ libutil/md5.c\
libutil/mode.c\ libutil/mode.c\
libutil/putword.c\ libutil/putword.c\

2
col.c
View File

@ -177,7 +177,7 @@ allocbuf(void)
{ {
char **bp; char **bp;
buff = emallocarray(pagsize, sizeof(*buff)); buff = ereallocarray(NULL, pagsize, sizeof(*buff));
for (bp = buff; bp < &buff[pagsize]; ++bp) for (bp = buff; bp < &buff[pagsize]; ++bp)
*bp = emalloc(NCOLS); *bp = emalloc(NCOLS);
} }

2
cut.c
View File

@ -56,7 +56,7 @@ parselist(char *str)
if (*s == ',') if (*s == ',')
n++; n++;
} }
r = emallocarray(n, sizeof(*r)); r = ereallocarray(NULL, n, sizeof(*r));
for (s = str; n; n--, s++) { for (s = str; n; n--, s++) {
r->min = (*s == '-') ? 1 : strtoul(s, &s, 10); r->min = (*s == '-') ? 1 : strtoul(s, &s, 10);
r->max = (*s == '-') ? strtoul(s + 1, &s, 10) : r->min; r->max = (*s == '-') ? strtoul(s + 1, &s, 10) : r->min;

8
find.c
View File

@ -595,7 +595,7 @@ get_exec_arg(char *argv[], Extra *extra)
e->u.p.arglen = e->u.p.filelen = 0; e->u.p.arglen = e->u.p.filelen = 0;
e->u.p.first = e->u.p.next = arg - argv - 1; e->u.p.first = e->u.p.next = arg - argv - 1;
e->u.p.cap = (arg - argv) * 2; 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++) { for (arg = argv, new = e->argv; *arg; arg++, new++) {
*new = *arg; *new = *arg;
@ -604,7 +604,7 @@ get_exec_arg(char *argv[], Extra *extra)
arg++; /* due to our extra NULL */ arg++; /* due to our extra NULL */
} else { } else {
e->argv = argv; 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++) for (arg = argv, braces = e->u.s.braces; *arg; arg++)
if (!strcmp(*arg, "{}")) if (!strcmp(*arg, "{}"))
@ -632,7 +632,7 @@ get_ok_arg(char *argv[], Extra *extra)
*arg = NULL; *arg = NULL;
o->argv = argv; 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++) for (arg = argv, braces = o->braces; *arg; arg++)
if (!strcmp(*arg, "{}")) if (!strcmp(*arg, "{}"))
@ -824,7 +824,7 @@ parse(int argc, char **argv)
* https://en.wikipedia.org/wiki/Shunting-yard_algorithm * https://en.wikipedia.org/wiki/Shunting-yard_algorithm
* read from infix, resulting rpn ends up in rpn, next position in rpn is out * 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 */ * 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++) { for (tok = infix, out = rpn, top = stack; tok->type != END; tok++) {
switch (tok->type) { switch (tok->type) {
case PRIM: *out++ = *tok; break; case PRIM: *out++ = *tok; break;

View File

@ -1,50 +0,0 @@
/*
* Copyright (c) 2008 Otto Moerbeek <otto@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;
}

2
ls.c
View File

@ -341,7 +341,7 @@ main(int argc, char *argv[])
if (argc == 0) if (argc == 0)
*--argv = ".", argc++; *--argv = ".", argc++;
ents = emallocarray(argc, sizeof(*ents)); ents = ereallocarray(NULL, argc, sizeof(*ents));
for (i = 0; i < argc; i++) for (i = 0; i < argc; i++)
mkent(&ents[i], argv[i], 1, Hflag || Lflag); mkent(&ents[i], argv[i], 1, Hflag || Lflag);

View File

@ -107,11 +107,11 @@ main(int argc, char *argv[])
/* populate delimiters */ /* populate delimiters */
unescape(adelim); unescape(adelim);
delim = emallocarray(utflen(adelim) + 1, sizeof(*delim)); delim = ereallocarray(NULL, utflen(adelim) + 1, sizeof(*delim));
len = utftorunestr(adelim, delim); len = utftorunestr(adelim, delim);
/* populate file list */ /* populate file list */
dsc = emallocarray(argc, sizeof(*dsc)); dsc = ereallocarray(NULL, argc, sizeof(*dsc));
for (i = 0; i < argc; i++) { for (i = 0; i < argc; i++) {
if (strcmp(argv[i], "-") == 0) if (strcmp(argv[i], "-") == 0)

View File

@ -111,7 +111,7 @@ main(int argc, char *argv[])
break; break;
case 'c': case 'c':
unescape(arg); unescape(arg);
rarg = emallocarray(utflen(arg) + 1, sizeof(*rarg)); rarg = ereallocarray(NULL, utflen(arg) + 1, sizeof(*rarg));
utftorunestr(arg, rarg); utftorunestr(arg, rarg);
efputrune(rarg, stdout, "<stdout>"); efputrune(rarg, stdout, "<stdout>");
free(rarg); free(rarg);
@ -125,7 +125,7 @@ main(int argc, char *argv[])
if (arg[j] == '\'' || arg[j] == '\"') { if (arg[j] == '\'' || arg[j] == '\"') {
arg += j + 1; arg += j + 1;
unescape(arg); unescape(arg);
rarg = emallocarray(utflen(arg) + 1, sizeof(*rarg)); rarg = ereallocarray(NULL, utflen(arg) + 1, sizeof(*rarg));
utftorunestr(arg, rarg); utftorunestr(arg, rarg);
num = rarg[0]; num = rarg[0];
} else } else

2
sed.c
View File

@ -598,7 +598,7 @@ strtorunes(char *s, size_t nrunes)
{ {
Rune *rs, *rp; Rune *rs, *rp;
rp = rs = emallocarray(nrunes + 1, sizeof(*rs)); rp = rs = ereallocarray(NULL, nrunes + 1, sizeof(*rs));
while (nrunes--) while (nrunes--)
s += chartorune(rp++, s); s += chartorune(rp++, s);

View File

@ -15,7 +15,7 @@ strings(FILE *fp, const char *fname, size_t len)
size_t i, bread; size_t i, bread;
off_t off; off_t off;
rbuf = emallocarray(len, sizeof(*rbuf)); rbuf = ereallocarray(NULL, len, sizeof(*rbuf));
for (off = 0, i = 0; (bread = efgetrune(&r, fp, fname)); ) { for (off = 0, i = 0; (bread = efgetrune(&r, fp, fname)); ) {
off += bread; off += bread;

4
tr.c
View File

@ -78,9 +78,9 @@ makeset(char *str, struct range **set, int (**check)(Rune))
/* rstr defines at most len ranges */ /* rstr defines at most len ranges */
unescape(str); unescape(str);
rstr = emallocarray(utflen(str) + 1, sizeof(*rstr)); rstr = ereallocarray(NULL, utflen(str) + 1, sizeof(*rstr));
len = utftorunestr(str, rstr); len = utftorunestr(str, rstr);
*set = emallocarray(len, sizeof(**set)); *set = ereallocarray(NULL, len, sizeof(**set));
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
if (rstr[i] == '[') { if (rstr[i] == '[') {

2
util.h
View File

@ -25,8 +25,6 @@ void apathmax(char **, size_t *);
void *ecalloc(size_t, size_t); void *ecalloc(size_t, size_t);
void *emalloc(size_t); void *emalloc(size_t);
void *mallocarray(size_t, size_t);
void *emallocarray(size_t, size_t);
void *erealloc(void *, size_t); void *erealloc(void *, size_t);
void *reallocarray(void *, size_t, size_t); void *reallocarray(void *, size_t, size_t);
void *ereallocarray(void *, size_t, size_t); void *ereallocarray(void *, size_t, size_t);