From d597be5bc1ee0e76117037b7d9a52c52fd4531ad Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Wed, 24 Apr 2024 11:07:33 -0400 Subject: eval: Plug memory leak if bfs_opendir() fails --- src/eval.c | 40 ++++++++++++++++++++++------------------ tests/common/empty.out | 8 ++++++++ tests/common/empty.sh | 1 + tests/common/empty_error.out | 3 +++ tests/common/empty_error.sh | 7 +++++++ tests/common/empty_special.out | 20 ++++++++++++++++++++ tests/common/empty_special.sh | 1 + tests/gnu/empty.out | 8 -------- tests/gnu/empty.sh | 1 - tests/gnu/empty_special.out | 20 -------------------- tests/gnu/empty_special.sh | 1 - 11 files changed, 62 insertions(+), 48 deletions(-) create mode 100644 tests/common/empty.out create mode 100644 tests/common/empty.sh create mode 100644 tests/common/empty_error.out create mode 100644 tests/common/empty_error.sh create mode 100644 tests/common/empty_special.out create mode 100644 tests/common/empty_special.sh delete mode 100644 tests/gnu/empty.out delete mode 100644 tests/gnu/empty.sh delete mode 100644 tests/gnu/empty_special.out delete mode 100644 tests/gnu/empty_special.sh diff --git a/src/eval.c b/src/eval.c index b103912..49028b7 100644 --- a/src/eval.c +++ b/src/eval.c @@ -445,38 +445,42 @@ bool eval_depth(const struct bfs_expr *expr, struct bfs_eval *state) { * -empty test. */ bool eval_empty(const struct bfs_expr *expr, struct bfs_eval *state) { - bool ret = false; const struct BFTW *ftwbuf = state->ftwbuf; + const struct bfs_stat *statbuf; + struct bfs_dir *dir; + + switch (ftwbuf->type) { + case BFS_REG: + statbuf = eval_stat(state); + return statbuf && statbuf->size == 0; - if (ftwbuf->type == BFS_DIR) { - struct bfs_dir *dir = bfs_allocdir(); + case BFS_DIR: + dir = bfs_allocdir(); if (!dir) { - eval_report_error(state); - return ret; + goto error; } if (bfs_opendir(dir, ftwbuf->at_fd, ftwbuf->at_path, 0) != 0) { - eval_report_error(state); - return ret; + goto error; } int did_read = bfs_readdir(dir, NULL); + bfs_closedir(dir); + if (did_read < 0) { - eval_report_error(state); - } else { - ret = !did_read; + goto error; } - bfs_closedir(dir); free(dir); - } else if (ftwbuf->type == BFS_REG) { - const struct bfs_stat *statbuf = eval_stat(state); - if (statbuf) { - ret = statbuf->size == 0; - } - } + return did_read == 0; + error: + eval_report_error(state); + free(dir); + return false; - return ret; + default: + return false; + } } /** diff --git a/tests/common/empty.out b/tests/common/empty.out new file mode 100644 index 0000000..a0f4b76 --- /dev/null +++ b/tests/common/empty.out @@ -0,0 +1,8 @@ +basic/a +basic/b +basic/c/d +basic/e/f +basic/g/h +basic/i +basic/j/foo +basic/k/foo/bar diff --git a/tests/common/empty.sh b/tests/common/empty.sh new file mode 100644 index 0000000..95ee988 --- /dev/null +++ b/tests/common/empty.sh @@ -0,0 +1 @@ +bfs_diff basic -empty diff --git a/tests/common/empty_error.out b/tests/common/empty_error.out new file mode 100644 index 0000000..da45e23 --- /dev/null +++ b/tests/common/empty_error.out @@ -0,0 +1,3 @@ +./bar +./baz +./qux diff --git a/tests/common/empty_error.sh b/tests/common/empty_error.sh new file mode 100644 index 0000000..7c8049c --- /dev/null +++ b/tests/common/empty_error.sh @@ -0,0 +1,7 @@ +cd "$TEST" + +"$XTOUCH" -p foo/ bar/ baz qux +chmod -r foo baz +defer chmod +r foo baz + +! bfs_diff . -empty diff --git a/tests/common/empty_special.out b/tests/common/empty_special.out new file mode 100644 index 0000000..fa35478 --- /dev/null +++ b/tests/common/empty_special.out @@ -0,0 +1,20 @@ +rainbow// +rainbow/exec.sh +rainbow/file.dat +rainbow/file.txt +rainbow/lower.gz +rainbow/lower.tar +rainbow/lower.tar.gz +rainbow/lu.tar.GZ +rainbow/mh1 +rainbow/mh2 +rainbow/ow +rainbow/sgid +rainbow/sticky +rainbow/sticky_ow +rainbow/sugid +rainbow/suid +rainbow/ul.TAR.gz +rainbow/upper.GZ +rainbow/upper.TAR +rainbow/upper.TAR.GZ diff --git a/tests/common/empty_special.sh b/tests/common/empty_special.sh new file mode 100644 index 0000000..31e9d2e --- /dev/null +++ b/tests/common/empty_special.sh @@ -0,0 +1 @@ +bfs_diff rainbow -empty diff --git a/tests/gnu/empty.out b/tests/gnu/empty.out deleted file mode 100644 index a0f4b76..0000000 --- a/tests/gnu/empty.out +++ /dev/null @@ -1,8 +0,0 @@ -basic/a -basic/b -basic/c/d -basic/e/f -basic/g/h -basic/i -basic/j/foo -basic/k/foo/bar diff --git a/tests/gnu/empty.sh b/tests/gnu/empty.sh deleted file mode 100644 index 95ee988..0000000 --- a/tests/gnu/empty.sh +++ /dev/null @@ -1 +0,0 @@ -bfs_diff basic -empty diff --git a/tests/gnu/empty_special.out b/tests/gnu/empty_special.out deleted file mode 100644 index fa35478..0000000 --- a/tests/gnu/empty_special.out +++ /dev/null @@ -1,20 +0,0 @@ -rainbow// -rainbow/exec.sh -rainbow/file.dat -rainbow/file.txt -rainbow/lower.gz -rainbow/lower.tar -rainbow/lower.tar.gz -rainbow/lu.tar.GZ -rainbow/mh1 -rainbow/mh2 -rainbow/ow -rainbow/sgid -rainbow/sticky -rainbow/sticky_ow -rainbow/sugid -rainbow/suid -rainbow/ul.TAR.gz -rainbow/upper.GZ -rainbow/upper.TAR -rainbow/upper.TAR.GZ diff --git a/tests/gnu/empty_special.sh b/tests/gnu/empty_special.sh deleted file mode 100644 index 31e9d2e..0000000 --- a/tests/gnu/empty_special.sh +++ /dev/null @@ -1 +0,0 @@ -bfs_diff rainbow -empty -- cgit v1.2.3