From: Todd C. Miller Date: Thu, 14 May 2015 16:21:58 +0000 (-0600) Subject: Use reallocarray where possible. X-Git-Tag: SUDO_1_8_14^2~130 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c75eb5bf0dda0669e524d1b3d557317e3cb47199;p=sudo Use reallocarray where possible. --- diff --git a/lib/util/getcwd.c b/lib/util/getcwd.c index ad0555f9a..44662732a 100644 --- a/lib/util/getcwd.c +++ b/lib/util/getcwd.c @@ -165,8 +165,9 @@ sudo_getcwd(char *pt, size_t size) if (bup + 3 + MAXNAMLEN + 1 >= eup) { char *nup; - if ((nup = realloc(up, upsize *= 2)) == NULL) + if ((nup = reallocarray(up, upsize, 2)) == NULL) goto err; + upsize *= 2; up = nup; bup = up; eup = up + upsize; @@ -228,8 +229,9 @@ sudo_getcwd(char *pt, size_t size) } off = bpt - pt; len = ept - bpt; - if ((npt = realloc(pt, ptsize *= 2)) == NULL) + if ((npt = reallocarray(pt, ptsize, 2)) == NULL) goto err; + ptsize *= 2; pt = npt; bpt = pt + off; ept = pt + ptsize; diff --git a/lib/util/getline.c b/lib/util/getline.c index 30d31e4b6..5e7ba5a61 100644 --- a/lib/util/getline.c +++ b/lib/util/getline.c @@ -89,10 +89,10 @@ sudo_getline(char **bufp, size_t *bufsizep, FILE *fp) len = strlen(buf); if (!len || buf[len - 1] == '\n' || feof(fp)) break; - bufsize *= 2; - cp = realloc(buf, bufsize); + cp = reallocarray(buf, bufsize, 2); if (cp == NULL) return -1; + bufsize *= 2; buf = cp; } *bufp = buf; diff --git a/lib/util/glob.c b/lib/util/glob.c index 6a58a3f04..1cb0574cc 100644 --- a/lib/util/glob.c +++ b/lib/util/glob.c @@ -779,7 +779,7 @@ globextend(const Char *path, glob_t *pglob, struct glob_lim *limitp, return GLOB_NOSPACE; } - pathv = realloc(pglob->gl_pathv, newn * sizeof(*pathv)); + pathv = reallocarray(pglob->gl_pathv, newn, sizeof(*pathv)); if (pathv == NULL) goto nospace; if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) { diff --git a/plugins/sudoers/env.c b/plugins/sudoers/env.c index 7079b00c0..64020a6e3 100644 --- a/plugins/sudoers/env.c +++ b/plugins/sudoers/env.c @@ -307,7 +307,7 @@ sudo_putenv_nodebug(char *str, bool dupcheck, bool overwrite) errno = EOVERFLOW; return -1; } - nenvp = realloc(env.envp, nsize * sizeof(char *)); + nenvp = reallocarray(env.envp, nsize, sizeof(char *)); if (nenvp == NULL) { errno = ENOMEM; return -1; diff --git a/plugins/sudoers/toke.c b/plugins/sudoers/toke.c index 5d11bfc2f..a5115a1ad 100644 --- a/plugins/sudoers/toke.c +++ b/plugins/sudoers/toke.c @@ -4090,7 +4090,7 @@ read_dir_files(const char *dirpath, struct path_list ***pathsp) if (count >= max_paths) { struct path_list **tmp; max_paths <<= 1; - tmp = realloc(paths, sizeof(*paths) * max_paths); + tmp = reallocarray(paths, max_paths, sizeof(*paths)); if (tmp == NULL) { sudo_efree(path); sudo_efree(pl); @@ -4197,8 +4197,7 @@ _push_include(char *path, bool isdir) debug_return_bool(false); } istacksize += SUDOERS_STACK_INCREMENT; - new_istack = (struct include_stack *) realloc(istack, - sizeof(*istack) * istacksize); + new_istack = reallocarray(istack, istacksize, sizeof(*istack)); if (new_istack == NULL) { sudo_warn(NULL); sudoerserror(NULL); diff --git a/plugins/sudoers/toke.l b/plugins/sudoers/toke.l index 4f0db7f8e..81829de99 100644 --- a/plugins/sudoers/toke.l +++ b/plugins/sudoers/toke.l @@ -819,7 +819,7 @@ read_dir_files(const char *dirpath, struct path_list ***pathsp) if (count >= max_paths) { struct path_list **tmp; max_paths <<= 1; - tmp = realloc(paths, sizeof(*paths) * max_paths); + tmp = reallocarray(paths, max_paths, sizeof(*paths)); if (tmp == NULL) { sudo_efree(path); sudo_efree(pl); @@ -926,8 +926,7 @@ _push_include(char *path, bool isdir) debug_return_bool(false); } istacksize += SUDOERS_STACK_INCREMENT; - new_istack = (struct include_stack *) realloc(istack, - sizeof(*istack) * istacksize); + new_istack = reallocarray(istack, istacksize, sizeof(*istack)); if (new_istack == NULL) { sudo_warn(NULL); sudoerserror(NULL);