From 7af0111c4ab667def52a6d108e43a13766d7c7fe Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Wed, 18 Feb 2026 19:48:30 +0100 Subject: [PATCH 1/2] Fix leak on error when writing content file --- cmdline/state.c | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/cmdline/state.c b/cmdline/state.c index 2f41b2fb..9a3557a9 100644 --- a/cmdline/state.c +++ b/cmdline/state.c @@ -3217,7 +3217,7 @@ static void* state_write_thread(void* arg) if (serror(f)) { /* LCOV_EXCL_START */ log_fatal(errno, "Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno)); - return context; + goto bail; /* LCOV_EXCL_STOP */ } @@ -3231,14 +3231,14 @@ static void* state_write_thread(void* arg) } else { /* LCOV_EXCL_START */ log_fatal(EINTERNAL, "Unexpected hash when writing the content file '%s'.\n", serrorfile(f)); - return context; + goto bail; /* LCOV_EXCL_STOP */ } swrite(state->hashseed, HASH_MAX, f); if (serror(f)) { /* LCOV_EXCL_START */ log_fatal(errno, "Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno)); - return context; + goto bail; /* LCOV_EXCL_STOP */ } @@ -3256,14 +3256,14 @@ static void* state_write_thread(void* arg) } else { /* LCOV_EXCL_START */ log_fatal(EINTERNAL, "Unexpected prevhash when writing the content file '%s'.\n", serrorfile(f)); - return context; + goto bail; /* LCOV_EXCL_STOP */ } swrite(state->prevhashseed, HASH_MAX, f); if (serror(f)) { /* LCOV_EXCL_START */ log_fatal(errno, "Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno)); - return context; + goto bail; /* LCOV_EXCL_STOP */ } } @@ -3279,7 +3279,7 @@ static void* state_write_thread(void* arg) if (!disk) { /* LCOV_EXCL_START */ log_fatal(EINTERNAL, "Internal inconsistency: Unmapped disk '%s'\n", map->name); - return context; + goto bail; /* LCOV_EXCL_STOP */ } @@ -3303,7 +3303,7 @@ static void* state_write_thread(void* arg) if (serror(f)) { /* LCOV_EXCL_START */ log_fatal(errno, "Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno)); - return context; + goto bail; /* LCOV_EXCL_STOP */ } } @@ -3342,7 +3342,7 @@ static void* state_write_thread(void* arg) if (serror(f)) { /* LCOV_EXCL_START */ log_fatal(errno, "Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno)); - return context; + goto bail; /* LCOV_EXCL_STOP */ } } @@ -3383,7 +3383,7 @@ static void* state_write_thread(void* arg) if (serror(f)) { /* LCOV_EXCL_START */ log_fatal(errno, "Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno)); - return context; + goto bail; /* LCOV_EXCL_STOP */ } @@ -3419,7 +3419,7 @@ static void* state_write_thread(void* arg) default : /* LCOV_EXCL_START */ log_fatal(EINTERNAL, "Internal inconsistency: State for block %u state %u\n", v_pos, v_state); - return context; + goto bail; /* LCOV_EXCL_STOP */ } @@ -3438,7 +3438,7 @@ static void* state_write_thread(void* arg) if (serror(f)) { /* LCOV_EXCL_START */ log_fatal(errno, "Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno)); - return context; + goto bail; /* LCOV_EXCL_STOP */ } @@ -3470,7 +3470,7 @@ static void* state_write_thread(void* arg) if (serror(f)) { /* LCOV_EXCL_START */ log_fatal(errno, "Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno)); - return context; + goto bail; /* LCOV_EXCL_STOP */ } } @@ -3485,7 +3485,7 @@ static void* state_write_thread(void* arg) if (serror(f)) { /* LCOV_EXCL_START */ log_fatal(errno, "Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno)); - return context; + goto bail; /* LCOV_EXCL_STOP */ } @@ -3498,7 +3498,7 @@ static void* state_write_thread(void* arg) if (serror(f)) { /* LCOV_EXCL_START */ log_fatal(errno, "Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno)); - return context; + goto bail; /* LCOV_EXCL_STOP */ } begin = 0; @@ -3542,7 +3542,7 @@ static void* state_write_thread(void* arg) if (serror(f)) { /* LCOV_EXCL_START */ log_fatal(errno, "Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno)); - return context; + goto bail; /* LCOV_EXCL_STOP */ } } @@ -3627,7 +3627,7 @@ static void* state_write_thread(void* arg) if (serror(f)) { /* LCOV_EXCL_START */ log_fatal(errno, "Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno)); - return context; + goto bail; /* LCOV_EXCL_STOP */ } @@ -3641,7 +3641,7 @@ static void* state_write_thread(void* arg) if (sflush(f)) { /* LCOV_EXCL_START */ log_fatal(errno, "Error writing the content file '%s' (in flush before crc). %s.\n", serrorfile(f), strerror(errno)); - return context; + goto bail; /* LCOV_EXCL_STOP */ } @@ -3655,7 +3655,7 @@ static void* state_write_thread(void* arg) log_fatal(ECONTENT, "CRC mismatch while writing the content stream.\n"); log_fatal(ECONTENT, "DANGER! Your RAM memory is faulty! DO NOT PROCEED UNTIL FIXED!\n"); log_fatal(ECONTENT, "Try running a memory test like http://www.memtest86.com/\n"); - return context; + goto bail; /* LCOV_EXCL_STOP */ } @@ -3663,7 +3663,7 @@ static void* state_write_thread(void* arg) if (serror(f)) { /* LCOV_EXCL_START */ log_fatal(errno, "Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno)); - return context; + goto bail; /* LCOV_EXCL_STOP */ } @@ -3681,9 +3681,13 @@ static void* state_write_thread(void* arg) /* store the bucket info list */ if (context->first) bucket_to_list(&bucket_hash, &state->bucketlist, &state->bucketcount); - tommy_hashdyn_done(&bucket_hash); + tommy_hashdyn_done(&bucket_hash); return 0; + +bail: + tommy_hashdyn_done(&bucket_hash); + return context; } static void state_write_content(struct snapraid_state* state, uint32_t* out_crc) From 575ce99b5fd9e1009dfb73f8b7cf82db39d31fc4 Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Wed, 18 Feb 2026 19:52:57 +0100 Subject: [PATCH 2/2] Use a 64 bit hash for the buckets, just to avoid conversion warnings --- cmdline/elem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmdline/elem.c b/cmdline/elem.c index a5049e0d..72150a60 100644 --- a/cmdline/elem.c +++ b/cmdline/elem.c @@ -1434,7 +1434,7 @@ void bucket_free(struct snapraid_bucket* bucket) void bucket_insert(tommy_hashdyn* bucket_hash, time_t time_at, block_off_t count, int justsynced) { - unsigned hash = tommy_inthash_u32(time_at); + tommy_hash_t hash = tommy_inthash_u64(time_at); tommy_node* i = tommy_hashdyn_bucket(bucket_hash, hash); while (i) { struct snapraid_bucket* entry = i->data;