]> granicus.if.org Git - sudo/commitdiff
Use reallocarray where possible.
authorTodd C. Miller <Todd.Miller@courtesan.com>
Thu, 14 May 2015 16:21:58 +0000 (10:21 -0600)
committerTodd C. Miller <Todd.Miller@courtesan.com>
Thu, 14 May 2015 16:21:58 +0000 (10:21 -0600)
lib/util/getcwd.c
lib/util/getline.c
lib/util/glob.c
plugins/sudoers/env.c
plugins/sudoers/toke.c
plugins/sudoers/toke.l

index ad0555f9ad8fe2ebd6a8bb6c4d23017a8f777549..44662732ab71c53c90c97d6b37dd793b5d3ce56e 100644 (file)
@@ -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;
index 30d31e4b6efa0a420b866479f5c0c169fd952e0a..5e7ba5a61db234140b818dcb4a7b4c30f41bd95e 100644 (file)
@@ -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;
index 6a58a3f04e7b3031c2ada8d81ee1740d5962fd07..1cb0574cc488f1522c5263efe11975679f7e7e60 100644 (file)
@@ -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) {
index 7079b00c0320a7aa64e1ed99f1e3b82e12186b16..64020a6e3be0fb5d8cb66e34cb986f0aa1d6f758 100644 (file)
@@ -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;
index 5d11bfc2f1b513494e304f4725a6c7ee47716d26..a5115a1adc98ff1b6fd8bc44cff0962873ed3e41 100644 (file)
@@ -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);
index 4f0db7f8e63d4e74f06e49159ed2bce284c0f767..81829de992570bb97e9083f4c531e73e8f83c26c 100644 (file)
@@ -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);