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

[PATCH] am: close stream on error, but not stdin

From
René Scharfe <l.s.r@web.de>
Date
Apr 16, 2017, 16:55 UTC
Message-ID
<309ca90b-9f44-c2d8-c377-98080369bc05@web.de>
Avoid closing stdin, but do close an actual input file on error exit.
Found with Cppcheck.
Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
 builtin/am.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/builtin/am.c b/builtin/am.c
index f7a7a971fb..805f56cec2 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -762,14 +762,18 @@ static int split_mail_conv(mail_conv_fn fn, struct am_state *state,
 		mail = mkpath("%s/%0*d", state->dir, state->prec, i + 1);
 
 		out = fopen(mail, "w");
-		if (!out)
+		if (!out) {
+			if (in != stdin)
+				fclose(in);
 			return error_errno(_("could not open '%s' for writing"),
 					   mail);
+		}
 
 		ret = fn(out, in, keep_cr);
 
 		fclose(out);
-		fclose(in);
+		if (in != stdin)
+			fclose(in);
 
 		if (ret)
 			return error(_("could not parse patch '%s'"), *paths);
-- 
2.12.2
Message 1 of 1 in “am: close stream on error, but not stdin”
  1. am: close stream on error, but not stdinRené Scharfe, Apr 16, 2017

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.