summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2021-10-19 12:11:11 -0400
committerTavian Barnes <tavianator@tavianator.com>2021-10-19 12:28:17 -0400
commitbe3c3ed7dfd0182ea11468c8d4dffa465ce7a44f (patch)
tree102bf8293b1dc684c2777fc1f9a6bacdb05bea06
parent32c54e2769c3e4c5ada44e6107745d0893e86c70 (diff)
downloadbfs-be3c3ed7dfd0182ea11468c8d4dffa465ce7a44f.tar.xz
parse: Fix UAF + double-free when ftruncate() fails
-rw-r--r--parse.c13
1 files changed, 8 insertions, 5 deletions
diff --git a/parse.c b/parse.c
index a7ee3ab..c257f4e 100644
--- a/parse.c
+++ b/parse.c
@@ -336,6 +336,7 @@ static int expr_open(struct parser_state *state, struct expr *expr, const char *
FILE *file = NULL;
CFILE *cfile = NULL;
+ CFILE *dedup = NULL;
file = xfopen(path, O_WRONLY | O_CREAT | O_CLOEXEC);
if (!file) {
@@ -347,7 +348,7 @@ static int expr_open(struct parser_state *state, struct expr *expr, const char *
goto fail;
}
- CFILE *dedup = bfs_ctx_dedup(ctx, cfile, path);
+ dedup = bfs_ctx_dedup(ctx, cfile, path);
if (!dedup) {
goto fail;
}
@@ -367,10 +368,12 @@ static int expr_open(struct parser_state *state, struct expr *expr, const char *
fail:
parse_error(state, "${blu}%s${rs} ${bld}%s${rs}: %m.\n", expr->argv[0], path);
- if (cfile) {
- cfclose(cfile);
- } else if (file) {
- fclose(file);
+ if (!dedup) {
+ if (cfile) {
+ cfclose(cfile);
+ } else if (file) {
+ fclose(file);
+ }
}
return -1;
}