From b0623182beff3521929a732ec767d460d90d3afb Mon Sep 17 00:00:00 2001 From: Avril Date: Sun, 11 Jul 2021 20:06:19 +0100 Subject: [PATCH] Added rollback to unmap previous haystack maps on error in `map_haystacks()` before returning. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fortune for naka's current commit: Future blessing − 末吉 --- include/macros.h | 7 +++++++ src/main.c | 12 ++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/include/macros.h b/include/macros.h index ce3e17f..55223af 100644 --- a/include/macros.h +++ b/include/macros.h @@ -89,6 +89,13 @@ _mixin void _drain_val(void* x, ...) { IGNORE(x); } // This compiles to no-op on // Function macros +// debug-only statements +#ifdef DEBUG +#define DBG(x) x +#else +#define DBG(x) ((void)0) +#endif + // Assertions #define assert_msg(expr, ...) ( (expr) ? 1 : FATAL(__VA_ARGS__)) diff --git a/src/main.c b/src/main.c index 0f103a6..855153b 100644 --- a/src/main.c +++ b/src/main.c @@ -48,14 +48,22 @@ noreturn void usage_then_exit(int err, int argc, char** argv) static int map_haystacks(const char* const * h, map_t maps[pOUT]) { const char* path; + usize d =0; while( (path = *h++) ) { map_t *pOUT c = maps++; INFO("Mapping haystack file `%s' (to map addr %p)", path, c); debug_assert(c); - if(!map_handle_err(map_file(path, false, 0, 0, c))) - //TODO: Handle unmapping previous haystacks before return + if(!map_handle_err(map_file(path, false, 0, 0, c))) { + //Handle unmapping previous haystacks before return + // Prep reset maps to before `c` for unwind + maps-=1; + ERROR("Failed to map file `%s', rolling back %lu maps. (erp: %p, prev: %p)", path, d, c, maps); + for(usize i=0;i