git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v3 6/6] midx-write: simplify error cases

From
Derrick Stolee via GitGitGadget <gitgitgadget@gmail.com>
Date
Sep 5, 2025, 19:26 UTC
Message-ID
<224be4ee5c66a5d161ba35754dd324b8ee53d07c.1757100378.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1965.v3.git.1757100378.gitgitgadget@gmail.com>
From: Derrick Stolee <stolee@gmail.com>

The write_midx_internal() method uses gotos to jump to a cleanup section to clear memory before returning 'result'. Since these jumps are more common for error conditions, initialize 'result' to -1 and then only set it to 0 before returning with success. There are a couple places where we return with success via a jump.

This has the added benefit that the method now returns -1 on error instead of an inconsistent 1 or -1.

Signed-off-by: Derrick Stolee <stolee@gmail.com>
---
 midx-write.c | 26 +++++++++-----------------
 1 file changed, 9 insertions(+), 17 deletions(-)
diff --git a/midx-write.c b/midx-write.c
index cd7bf7554a..72189f74bb 100644
--- a/midx-write.c
+++ b/midx-write.c
@@ -1046,7 +1046,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
 	int bitmapped_packs_concat_len = 0;
 	int pack_name_concat_len = 0;
 	int dropped_packs = 0;
-	int result = 0;
+	int result = -1;
 	const char **keep_hashes = NULL;
 	struct chunkfile *cf;
 
@@ -1099,14 +1099,12 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
 				error(_("could not load reverse index for MIDX %s"),
 				      hash_to_hex_algop(get_midx_checksum(m),
 							m->repo->hash_algo));
-				result = 1;
 				goto cleanup;
 			}
 			ctx.num_multi_pack_indexes_before++;
 			m = m->base_midx;
 		}
 	} else if (ctx.m && fill_packs_from_midx(&ctx)) {
-		result = 1;
 		goto cleanup;
 	}
 
@@ -1142,12 +1140,16 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
 			 */
 			if (!want_bitmap)
 				clear_midx_files_ext(object_dir, "bitmap", NULL);
+
+			result = 0;
 			goto cleanup;
 		}
 	}
 
-	if (ctx.incremental && !ctx.nr)
+	if (ctx.incremental && !ctx.nr) {
+		result = 0;
 		goto cleanup; /* nothing to do */
+	}
 
 	if (preferred_pack_name) {
 		ctx.preferred_pack_idx = NO_PREFERRED_PACK;
@@ -1221,7 +1223,6 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
 		if (!preferred->num_objects) {
 			error(_("cannot select preferred pack %s with no objects"),
 			      preferred->pack_name);
-			result = 1;
 			goto cleanup;
 		}
 	}
@@ -1260,10 +1261,8 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
 			}
 		}
 
-		if (missing_drops) {
-			result = 1;
+		if (missing_drops)
 			goto cleanup;
-		}
 	}
 
 	/*
@@ -1309,7 +1308,6 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
 
 	if (ctx.nr - dropped_packs == 0) {
 		error(_("no pack files to index."));
-		result = 1;
 		goto cleanup;
 	}
 
@@ -1329,14 +1327,12 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
 		incr = mks_tempfile_m(midx_name.buf, 0444);
 		if (!incr) {
 			error(_("unable to create temporary MIDX layer"));
-			result = -1;
 			goto cleanup;
 		}
 
 		if (adjust_shared_perm(r, get_tempfile_path(incr))) {
 			error(_("unable to adjust shared permissions for '%s'"),
 			      get_tempfile_path(incr));
-			result = -1;
 			goto cleanup;
 		}
 
@@ -1414,7 +1410,6 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
 				      midx_hash, &pdata, commits, commits_nr,
 				      flags) < 0) {
 			error(_("could not write multi-pack bitmap"));
-			result = 1;
 			clear_packing_data(&pdata);
 			free(commits);
 			goto cleanup;
@@ -1440,21 +1435,17 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
 
 		if (!chainf) {
 			error_errno(_("unable to open multi-pack-index chain file"));
-			result = -1;
 			goto cleanup;
 		}
 
-		if (link_midx_to_chain(ctx.base_midx) < 0) {
-			result = -1;
+		if (link_midx_to_chain(ctx.base_midx) < 0)
 			goto cleanup;
-		}
 
 		get_split_midx_filename_ext(r->hash_algo, &final_midx_name,
 					    object_dir, midx_hash, MIDX_EXT_MIDX);
 
 		if (rename_tempfile(&incr, final_midx_name.buf) < 0) {
 			error_errno(_("unable to rename new multi-pack-index layer"));
-			result = -1;
 			goto cleanup;
 		}
 
@@ -1487,6 +1478,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
 	clear_midx_files(r, object_dir, keep_hashes,
 			 ctx.num_multi_pack_indexes_before + 1,
 			 ctx.incremental);
+	result = 0;
 
 cleanup:
 	for (size_t i = 0; i < ctx.nr; i++) {
-- 
gitgitgadget
Previous: Derrick Stolee via GitGitGadgetNext: Junio C Hamano
Message 43 of 47 in “midx-write: fix segfault and do several cleanups”
  1. 0/5 midx-write: fix segfault and do several cleanupsDerrick Stolee via GitGitGadget, Aug 28, 2025
  2. 1/5 midx-write: only load initialized packsDerrick Stolee via GitGitGadget, Aug 28, 2025
  3. Junio C HamanoAug 28, 2025
  4. Taylor BlauAug 29, 2025
  5. Derrick StoleeAug 30, 2025
  6. 2/5 midx-write: put failing response value backDerrick Stolee via GitGitGadget, Aug 28, 2025
  7. Junio C HamanoAug 28, 2025
  8. Taylor BlauAug 29, 2025
  9. 3/5 midx-write: use cleanup when incremental midx failsDerrick Stolee via GitGitGadget, Aug 28, 2025
  10. Junio C HamanoAug 28, 2025
  11. Taylor BlauAug 29, 2025
  12. Derrick StoleeAug 30, 2025
  13. 4/5 midx-write: use uint32_t for preferred_pack_idxDerrick Stolee via GitGitGadget, Aug 28, 2025
  14. Junio C HamanoAug 28, 2025
  15. Taylor BlauAug 29, 2025
  16. 5/5 midx-write: reenable signed comparison errorsDerrick Stolee via GitGitGadget, Aug 28, 2025
  17. Junio C HamanoAug 28, 2025
  18. Taylor BlauAug 29, 2025
  19. Taylor BlauAug 29, 2025
  20. 0/6 midx-write: fix segfault and do several cleanupsDerrick Stolee via GitGitGadget, Aug 30, 2025
  21. 1/6 midx-write: only load initialized packsDerrick Stolee via GitGitGadget, Aug 30, 2025
  22. Patrick SteinhardtSep 3, 2025
  23. Derrick StoleeSep 5, 2025
  24. 2/6 midx-write: put failing response value backDerrick Stolee via GitGitGadget, Aug 30, 2025
  25. Patrick SteinhardtSep 3, 2025
  26. Derrick StoleeSep 5, 2025
  27. 3/6 midx-write: use cleanup when incremental midx failsDerrick Stolee via GitGitGadget, Aug 30, 2025
  28. Patrick SteinhardtSep 3, 2025
  29. 4/6 midx-write: use uint32_t for preferred_pack_idxDerrick Stolee via GitGitGadget, Aug 30, 2025
  30. Patrick SteinhardtSep 3, 2025
  31. Derrick StoleeSep 5, 2025
  32. 5/6 midx-write: reenable signed comparison errorsDerrick Stolee via GitGitGadget, Aug 30, 2025
  33. Patrick SteinhardtSep 3, 2025
  34. 6/6 midx-write: simplify error casesDerrick Stolee via GitGitGadget, Aug 30, 2025
  35. Patrick SteinhardtSep 3, 2025
  36. Junio C HamanoSep 3, 2025
  37. 0/6 midx-write: fix segfault and do several cleanupsDerrick Stolee via GitGitGadget, Sep 5, 2025
  38. 1/6 midx-write: only load initialized packsDerrick Stolee via GitGitGadget, Sep 5, 2025
  39. 2/6 midx-write: put failing response value backDerrick Stolee via GitGitGadget, Sep 5, 2025
  40. 3/6 midx-write: use cleanup when incremental midx failsDerrick Stolee via GitGitGadget, Sep 5, 2025
  41. 4/6 midx-write: use uint32_t for preferred_pack_idxDerrick Stolee via GitGitGadget, Sep 5, 2025
  42. 5/6 midx-write: reenable signed comparison errorsDerrick Stolee via GitGitGadget, Sep 5, 2025
  43. 6/6 midx-write: simplify error casesDerrick Stolee via GitGitGadget, Sep 5, 2025
  44. Junio C HamanoSep 5, 2025
  45. Derrick StoleeSep 5, 2025
  46. Taylor BlauSep 11, 2025
  47. Junio C HamanoSep 11, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.