From 64ab988d34f95a7d9482152a74592ce32647d983 Mon Sep 17 00:00:00 2001 From: Tobias Kortkamp Date: Fri, 24 Jun 2016 13:29:08 +0200 Subject: [PATCH] Change realloc() back to reallocarray() --- Makefile | 2 +- doas.h | 6 +----- env.c | 2 +- parse.y | 12 +++--------- 4 files changed, 6 insertions(+), 16 deletions(-) diff --git a/Makefile b/Makefile index dc3bd24..d7a363d 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ CC?=clang YC?=yacc BIN=doas PREFIX?=/usr/local -OBJECTS=doas.o env.o execvpe.o y.tab.o +OBJECTS=doas.o env.o execvpe.o reallocarray.o y.tab.o CFLAG+= -DUSE_PAM LFLAG+= -lpam diff --git a/doas.h b/doas.h index c12ea1b..2ebf549 100644 --- a/doas.h +++ b/doas.h @@ -27,11 +27,7 @@ char **prepenv(struct rule *); #define _PW_NAME_LEN 32 #endif -#if !defined(HAVE_REALLOCARRAY) && !defined(HAVE_REALLOCARR) -int reallocarr(void *ptr, size_t num, size_t size); -#endif /* !HAVE_REALLOCARRAY && !HAVE_REALLOCARR */ - - +void *reallocarray(void *ptr, size_t nmemb, size_t size); #if !defined(HAVE_EXECVPE) int execvpe(const char *file, char * const *argv, char * const *envp); diff --git a/env.c b/env.c index 2b795d7..db8788b 100644 --- a/env.c +++ b/env.c @@ -95,7 +95,7 @@ flattenenv(struct env *env) struct envnode *node; u_int i; - envp = realloc(NULL, (env->count + 1) * sizeof(char *)); + envp = reallocarray(NULL, (env->count + 1), sizeof(char *)); if (!envp) err(1, NULL); i = 0; diff --git a/parse.y b/parse.y index d1e8d47..702b457 100644 --- a/parse.y +++ b/parse.y @@ -84,10 +84,8 @@ rule: action ident target cmd { maxrules = 63; else maxrules *= 2; - /* if (!(rules = reallocarray(rules, maxrules, + if (!(rules = reallocarray(rules, maxrules, sizeof(*rules)))) - */ - if (!(rules = realloc(rules, maxrules * sizeof(*rules)))) errx(1, "can't allocate rules"); } rules[nrules++] = r; @@ -127,10 +125,8 @@ envlist: /* empty */ { errx(1, "can't allocate envlist"); } | envlist TSTRING { int nenv = arraylen($1.envlist); - /* if (!($$.envlist = reallocarray($1.envlist, nenv + 2, + if (!($$.envlist = reallocarray($1.envlist, nenv + 2, sizeof(char *)))) - */ - if (!($$.envlist = realloc($1.envlist, (nenv + 2) * sizeof(char*)))) errx(1, "can't allocate envlist"); $$.envlist[nenv] = $2.str; $$.envlist[nenv + 1] = NULL; @@ -166,10 +162,8 @@ argslist: /* empty */ { errx(1, "can't allocate args"); } | argslist TSTRING { int nargs = arraylen($1.cmdargs); - /* if (!($$.cmdargs = reallocarray($1.cmdargs, nargs + 2, + if (!($$.cmdargs = reallocarray($1.cmdargs, nargs + 2, sizeof(char *)))) - */ - if (!($$.cmdargs = realloc($1.cmdargs, (nargs + 2) * sizeof(char *)))) errx(1, "can't allocate args"); $$.cmdargs[nargs] = $2.str; $$.cmdargs[nargs + 1] = NULL;