From 5163140694907a7070a7ffa8baec57014bde1dfd Mon Sep 17 00:00:00 2001 From: Evan Gates Date: Wed, 5 Oct 2016 15:34:52 -0700 Subject: [PATCH] find: remove VLAs --- find.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/find.c b/find.c index 5f75735..b331486 100644 --- a/find.c +++ b/find.c @@ -770,7 +770,9 @@ find_op(char *name) static void parse(int argc, char **argv) { - struct tok infix[2 * argc + 1], *stack[argc], *tok, *rpn, *out, **top; + struct tok *tok, *rpn, *out, **top, + *infix = emalloc((2 * argc + 1) * sizeof(*infix)), + **stack = emalloc(argc * sizeof(*stack)); struct op_info *op; struct pri_info *pri; char **arg; @@ -887,6 +889,9 @@ parse(int argc, char **argv) toks = rpn; root = *top; + + free(infix); + free(stack); } /* for a primary, run and return result @@ -925,8 +930,9 @@ find(char *path, struct findhist *hist) DIR *dir; struct dirent *de; struct findhist *f, cur; - size_t len = strlen(path) + 2; /* null and '/' */ + size_t namelen, pathcap = 0, len = strlen(path) + 2; /* null and '/' */ struct arg arg = { path, &st, { NULL } }; + char *p, *pathbuf = NULL; if ((gflags.l || (gflags.h && !hist) ? stat(path, &st) : lstat(path, &st)) < 0) { weprintf("failed to stat %s:", path); @@ -968,18 +974,20 @@ find(char *path, struct findhist *hist) } while (errno = 0, (de = readdir(dir))) { - size_t pathcap = len + strlen(de->d_name); - char pathbuf[pathcap], *p; - if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) continue; - + namelen = strlen(de->d_name); + if (len + namelen > pathcap) { + pathcap = len + namelen; + pathbuf = erealloc(pathbuf, pathcap); + } p = pathbuf + estrlcpy(pathbuf, path, pathcap); if (*--p != '/') estrlcat(pathbuf, "/", pathcap); estrlcat(pathbuf, de->d_name, pathcap); find(pathbuf, &cur); } + free(pathbuf); if (errno) { weprintf("readdir %s:", path); closedir(dir); -- 2.10.0