From 887b1dc7f28944fcfab76558928e526cbf06b8a0 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 6 Aug 2021 17:58:44 -0700 Subject: Properly set modifying_file/backup_file in case of errors --- bp.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'bp.c') diff --git a/bp.c b/bp.c index 3195885..8f805ef 100644 --- a/bp.c +++ b/bp.c @@ -290,7 +290,14 @@ static int process_file(def_t *defs, const char *filename, pat_t *pattern) f = copy; } FILE *out = fopen(filename, "w"); - matches += print_matches(out, defs, f, pattern); + // Set these temporary values in case the program crashes while in the + // middle of inplace modifying a file. If that happens, these variables + // are used to restore the original file contents. + modifying_file = out; backup_file = f; + { + matches += print_matches(out, defs, f, pattern); + } + modifying_file = NULL; backup_file = NULL; fclose(out); } else { matches += print_matches(stdout, defs, f, pattern); -- cgit v1.2.3