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

[PATCH] Fix export_marks() error handling.

From
MAMatthias Andree <matthias.andree@gmx.de>
Date
Jul 9, 2009, 13:28 UTC
Message-ID
<1247146081-4692-1-git-send-email-matthias.andree@gmx.de>
In-Reply-To
<alpine.DEB.1.00.0907091500420.4339@intel-tinevez-2-302>
- Don't leak one FILE * on error per export_marks() call. Found with
  cppcheck and reported by Martin Ettl.
- Abort the potentially long for(;idnums.size;) loop on write errors.
- Add a trailing full-stop to error message when fopen() fails.
Signed-off-by: Matthias Andree <matthias.andree@gmx.de>
---
 builtin-fast-export.c |   15 ++++++++++++---
 1 files changed, 12 insertions(+), 3 deletions(-)
diff --git a/builtin-fast-export.c b/builtin-fast-export.c
index 9a8a6fc..6c0956d 100644
--- a/builtin-fast-export.c
+++ b/builtin-fast-export.c
@@ -428,21 +428,30 @@ static void export_marks(char *file)
 	uint32_t mark;
 	struct object_decoration *deco = idnums.hash;
 	FILE *f;
+	int e;
 
 	f = fopen(file, "w");
 	if (!f)
-		error("Unable to open marks file %s for writing", file);
+		error("Unable to open marks file %s for writing.", file);
 
 	for (i = 0; i < idnums.size; i++) {
 		if (deco->base && deco->base->type == 1) {
 			mark = ptr_to_mark(deco->decoration);
-			fprintf(f, ":%"PRIu32" %s\n", mark,
+			e = fprintf(f, ":%"PRIu32" %s\n", mark,
 				sha1_to_hex(deco->base->sha1));
+			if (e < 0) break;
 		}
 		deco++;
 	}
 
-	if (ferror(f) || fclose(f))
+	/* do not optimize the next two lines - they must both be executed in
+	 * this order. || might short-circuit the fclose(), and combining them
+	 * into one statement might reverse the order of execution.
+	 * Also, fflush() may not be sufficient - on some file systems, the
+	 * error is still delayed until the final [f]close().  */
+	e  = ferror(f);
+	e |= fclose(f);
+	if (e)
 		error("Unable to write marks file %s.", file);
 }
 
-- 
1.6.3.3.385.g60647
Previous: Johannes SchindelinNext: Stephen R. van den Berg
Message 7 of 10 in “found a resource leak in file builtin-fast-export.c”
  1. Martin EttlJul 9, 2009
  2. Thomas RastJul 9, 2009
  3. Johannes SchindelinJul 9, 2009
  4. Thomas RastJul 9, 2009
  5. Andreas EricssonJul 9, 2009
  6. Johannes SchindelinJul 9, 2009
  7. Fix export_marks() error handling.Matthias Andree, Jul 9, 2009
  8. Stephen R. van den BergJul 11, 2009
  9. Matthias AndreeJul 13, 2009
  10. Matthias AndreeJul 9, 2009

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.