From 037a234953f16b53ba9cf52fcd1bb95fc16673d4 Mon Sep 17 00:00:00 2001 From: Avril Date: Mon, 12 Jul 2021 20:02:18 +0100 Subject: [PATCH] Unmap needle before returning from main after failed haystack maps. The process return value is still `PROG_RET_MAP_HAYSTACK_FAILED`, but a warning message is printed if unmapping the needle failed after the rollback. (this is unlikely) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fortune for naka's current commit: Half curse − 半凶 --- src/main.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main.c b/src/main.c index 775667a..7370ba4 100644 --- a/src/main.c +++ b/src/main.c @@ -137,6 +137,7 @@ inv_args: else if(!argv[2]) goto inv_args; + int m_rval =0; map_t needle; INFO("Mapping needle file `%s'", argv[1]); if(!map_handle_err(map_file(argv[1], false, 0, 0, &needle))) return PROG_RET_MAP_NEEDLE_FAILED; @@ -148,7 +149,11 @@ inv_args: usize hsn = AS(argc-2, usize); map_t haystacks[hsn]; TRACE("Attempting to map %lu haystacks", hsn); - if(!map_haystacks((const char**)(argv+1), haystacks)) return PROG_RET_MAP_HAYSTACK_FAILED; + if(!map_haystacks((const char**)(argv+1), haystacks)) { + // Unmap needle before return. + if(!map_handle_err(map_free(needle))) WARN("Failed to unmap needle before exiting after failed haystack maps"); + return PROG_RET_MAP_HAYSTACK_FAILED; + } #ifdef _FEATURE_PARALLEL // Multi-threaded TODO("Multithreaded processing currently unimplemented."); @@ -170,7 +175,6 @@ inv_args: free(full_result); #endif - int m_rval =0; for(int i=0;i<(int)hsn;i++) { INFO("Unmapping haystack haystack file `%s' (#%d) (map addr %p)", argv[i+1], i+1, haystacks+i); if(!map_handle_err(map_free(haystacks[i]))) m_rval = !m_rval ? PROG_RET_UNMAP_HAYSTACK_N_FAILED(i+1) : m_rval;