Merge pull request #1 from t6/reallocarray

Import reallocarray implementation from OpenBSD
This commit is contained in:
Jesse Smith 2016-06-24 09:39:40 -03:00 committed by GitHub
commit 5559ab3b42
6 changed files with 53 additions and 27 deletions

View file

@ -1,26 +1,23 @@
CC?=clang
YC?=yacc
YACC?=yacc
BIN=doas
PREFIX?=/usr/local
OBJECTS=doas.o env.o execvpe.o y.tab.o
CFLAG+= -DUSE_PAM
LFLAG+= -lpam
OBJECTS=doas.o env.o execvpe.o reallocarray.o y.tab.o
CFLAGS+=-DUSE_PAM -DDOAS_CONF=\"${PREFIX}/etc/doas.conf\"
LDFLAGS+=-lpam
all: $(OBJECTS)
$(CC) -o $(BIN) $(LFLAG) $(OBJECTS)
$(CC) -o $(BIN) $(LDFLAGS) $(OBJECTS)
env.o: doas.h env.c
$(CC) -c env.c
env.o: doas.h env.c
execvpe.o: doas.h execvpe.c
$(CC) -c execvpe.c
doas.o: doas.h doas.c parse.y
$(CC) $(CFLAG) -c doas.c
y.tab.o: parse.y
$(YC) parse.y
$(CC) -c y.tab.c
$(YACC) parse.y
$(CC) $(CFLAGS) -c y.tab.c
install: all
cp $(BIN) $(PREFIX)/bin/

2
doas.c
View file

@ -324,7 +324,7 @@ main(int argc, char **argv)
int pam_silent = PAM_SILENT;
#endif
parseconfig("/usr/local/etc/doas.conf", 1);
parseconfig(DOAS_CONF, 1);
/* cmdline is used only for logging, no need to abort on truncate */
(void) strlcpy(cmdline, argv[0], sizeof(cmdline));

6
doas.h
View file

@ -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);

2
env.c
View file

@ -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;

12
parse.y
View file

@ -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;

39
reallocarray.c Normal file
View file

@ -0,0 +1,39 @@
/* $OpenBSD: reallocarray.c,v 1.3 2015/09/13 08:31:47 guenther Exp $ */
/*
* 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>
/*
* 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 ((size_t)1 << (sizeof(size_t) * 4))
void *
reallocarray(void *optr, 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 realloc(optr, size * nmemb);
}
//DEF_WEAK(reallocarray);