am: close stream on error, but not stdin
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> Reviewed-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Apr 16, 2017 at 18:55 UTC
ac8ce18d89acccaa7a66900adff75d4aeb6ec80b
1 file changed
+6
-2
builtin/am.c
+6
-2
@@ -762,14 +762,18 @@ static int split_mail_conv(mail_conv_fn fn, struct am_state *state,
762
mail = mkpath("%s/%0*d", state->dir, state->prec, i + 1);
763
764
out = fopen(mail, "w");
765
- if (!out)
765
+ if (!out) {
766
+ if (in != stdin)
767
+ fclose(in);
768
return error_errno(_("could not open '%s' for writing"),
769
mail);
770
+ }
771
772
ret = fn(out, in, keep_cr);
773
774
fclose(out);
772
- fclose(in);
775
+ if (in != stdin)
776
+ fclose(in);
777
778
if (ret)
779
return error(_("could not parse patch '%s'"), *paths);