From 775d22150812256492b89409496930912fc44a1d Mon Sep 17 00:00:00 2001 From: espie Date: Sun, 18 May 2014 08:10:00 +0000 Subject: [PATCH] use reallocarray okay chl@, tedu@ --- usr.bin/find/extern.h | 5 +++-- usr.bin/find/function.c | 18 +++++++++--------- usr.bin/find/main.c | 6 +++--- usr.bin/find/misc.c | 14 ++++++++++++-- 4 files changed, 27 insertions(+), 16 deletions(-) diff --git a/usr.bin/find/extern.h b/usr.bin/find/extern.h index 85efd1cb0e5..b1d90ab402b 100644 --- a/usr.bin/find/extern.h +++ b/usr.bin/find/extern.h @@ -1,4 +1,4 @@ -/* * $OpenBSD: extern.h,v 1.18 2012/12/05 23:20:25 deraadt Exp $*/ +/* * $OpenBSD: extern.h,v 1.19 2014/05/18 08:10:00 espie Exp $*/ /*- * Copyright (c) 1991, 1993 * The Regents of the University of California. All rights reserved. @@ -31,7 +31,8 @@ */ void brace_subst(char *, char **, char *, int); -void *emalloc(unsigned int); +void *emalloc(size_t); +void *ereallocarray(void *, size_t, size_t); PLAN *find_create(char ***); int find_execute(PLAN *, char **); PLAN *find_formplan(char **); diff --git a/usr.bin/find/function.c b/usr.bin/find/function.c index de0ba09295a..cb9542ecff8 100644 --- a/usr.bin/find/function.c +++ b/usr.bin/find/function.c @@ -1,4 +1,4 @@ -/* $OpenBSD: function.c,v 1.40 2013/04/20 04:52:24 deraadt Exp $ */ +/* $OpenBSD: function.c,v 1.41 2014/05/18 08:10:00 espie Exp $ */ /*- * Copyright (c) 1990, 1993 @@ -537,8 +537,8 @@ c_exec(char *unused, char ***argvp, int isok) cnt = ap - *argvp - 1; /* units are words */ new->ep_maxargs = 5000; - new->e_argv = (char **)emalloc((u_int)(cnt + new->ep_maxargs) - * sizeof(char **)); + new->e_argv = ereallocarray(NULL, + (size_t)(cnt + new->ep_maxargs), sizeof(char **)); /* We start stuffing arguments after the user's last one. */ new->ep_bxp = &new->e_argv[cnt]; @@ -566,9 +566,9 @@ c_exec(char *unused, char ***argvp, int isok) new->ep_rval = 0; } else { /* !F_PLUSSET */ cnt = ap - *argvp + 1; - new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *)); - new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *)); - new->e_len = (int *)emalloc((u_int)cnt * sizeof(int)); + new->e_argv = ereallocarray(NULL, cnt, sizeof(char *)); + new->e_orig = ereallocarray(NULL, cnt, sizeof(char *)); + new->e_len = ereallocarray(NULL, cnt, sizeof(int)); for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) { new->e_orig[cnt] = *argv; @@ -685,9 +685,9 @@ c_execdir(char *ignored, char ***argvp, int unused) } cnt = ap - *argvp + 1; - new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *)); - new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *)); - new->e_len = (int *)emalloc((u_int)cnt * sizeof(int)); + new->e_argv = ereallocarray(NULL, cnt, sizeof(char *)); + new->e_orig = ereallocarray(NULL, cnt, sizeof(char *)); + new->e_len = ereallocarray(NULL, cnt, sizeof(int)); for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) { new->e_orig[cnt] = *argv; diff --git a/usr.bin/find/main.c b/usr.bin/find/main.c index 2a0b28f479e..e40b345c2b7 100644 --- a/usr.bin/find/main.c +++ b/usr.bin/find/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.29 2013/11/15 22:20:04 millert Exp $ */ +/* $OpenBSD: main.c,v 1.30 2014/05/18 08:10:00 espie Exp $ */ /*- * Copyright (c) 1990, 1993 @@ -67,7 +67,7 @@ main(int argc, char *argv[]) (void)time(&now); /* initialize the time-of-day */ - p = paths = (char **) emalloc(sizeof(char *) * argc); + p = paths = ereallocarray(NULL, argc, sizeof(char *)); sigaction(SIGINFO, &sa, NULL); @@ -121,7 +121,7 @@ main(int argc, char *argv[]) usage(); *p = NULL; - if (!(paths2 = realloc(paths, sizeof(char *) * (p - paths + 1)))) + if (!(paths2 = reallocarray(paths, p - paths + 1, sizeof(char *)))) err(1, NULL); paths = paths2; diff --git a/usr.bin/find/misc.c b/usr.bin/find/misc.c index 95bec3833b3..37e61a9aea0 100644 --- a/usr.bin/find/misc.c +++ b/usr.bin/find/misc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: misc.c,v 1.11 2009/10/27 23:59:38 deraadt Exp $ */ +/* $OpenBSD: misc.c,v 1.12 2014/05/18 08:10:00 espie Exp $ */ /*- * Copyright (c) 1990, 1993 @@ -115,7 +115,7 @@ queryuser(char **argv) * malloc with error checking. */ void * -emalloc(u_int len) +emalloc(size_t len) { void *p; @@ -124,6 +124,16 @@ emalloc(u_int len) err(1, NULL); } +void * +ereallocarray(void *oldp, size_t sz1, size_t sz2) +{ + void *p; + + if ((p = reallocarray(oldp, sz1, sz2)) != NULL) + return (p); + err(1, NULL); +} + /* * show_path -- * called on SIGINFO -- 2.20.1