{"thread":{"id":"45711","subject":"[PATCH] am: close stream on error, but not stdin","startedAt":"2017-04-16T16:55:45Z","lastAt":"2017-04-16T16:55:45Z","messageCount":1,"participants":["René Scharfe"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"316951","messageId":"309ca90b-9f44-c2d8-c377-98080369bc05@web.de","threadId":"45711","inReplyTo":null,"subject":"[PATCH] am: close stream on error, but not stdin","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2017-04-16T16:55:30Z","receivedAt":"2017-04-16T16:55:45Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Avoid closing stdin, but do close an actual input file on error exit.\n\nFound with Cppcheck.\n\nSigned-off-by: Rene Scharfe <l.s.r@web.de>\n---\n builtin/am.c | 8 ++++++--\n 1 file changed, 6 insertions(+), 2 deletions(-)\n\ndiff --git a/builtin/am.c b/builtin/am.c\nindex f7a7a971fb..805f56cec2 100644\n--- a/builtin/am.c\n+++ b/builtin/am.c\n@@ -762,14 +762,18 @@ static int split_mail_conv(mail_conv_fn fn, struct am_state *state,\n \t\tmail = mkpath(\"%s/%0*d\", state->dir, state->prec, i + 1);\n \n \t\tout = fopen(mail, \"w\");\n-\t\tif (!out)\n+\t\tif (!out) {\n+\t\t\tif (in != stdin)\n+\t\t\t\tfclose(in);\n \t\t\treturn error_errno(_(\"could not open '%s' for writing\"),\n \t\t\t\t\t   mail);\n+\t\t}\n \n \t\tret = fn(out, in, keep_cr);\n \n \t\tfclose(out);\n-\t\tfclose(in);\n+\t\tif (in != stdin)\n+\t\t\tfclose(in);\n \n \t\tif (ret)\n \t\t\treturn error(_(\"could not parse patch '%s'\"), *paths);\n-- \n2.12.2\n\n"}]}