summaryrefslogtreecommitdiffstats
path: root/src/eval.c
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2024-04-24 11:07:33 -0400
committerTavian Barnes <tavianator@tavianator.com>2024-04-24 11:08:10 -0400
commitd597be5bc1ee0e76117037b7d9a52c52fd4531ad (patch)
treea8384fcb4cabde1da989b8c3e3b114b8114dfcc0 /src/eval.c
parent30e309de41aaca259c4d0a200daa42ba74cbc5a5 (diff)
downloadbfs-d597be5bc1ee0e76117037b7d9a52c52fd4531ad.tar.xz
eval: Plug memory leak if bfs_opendir() fails
Diffstat (limited to 'src/eval.c')
-rw-r--r--src/eval.c40
1 files changed, 22 insertions, 18 deletions
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;
+ }
}
/**