{"thread":{"id":"61272","subject":"[PATCH] apply: don't leak fd on fdopen() error","startedAt":"2024-04-05T10:58:18Z","lastAt":"2024-04-05T17:09:18Z","messageCount":2,"participants":["René Scharfe","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"492291","messageId":"5ba55ee4-94c7-4094-a744-584fc623b391@web.de","threadId":"61272","inReplyTo":null,"subject":"[PATCH] apply: don't leak fd on fdopen() error","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2024-04-05T10:58:16Z","receivedAt":"2024-04-05T10:58:18Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Signed-off-by: René Scharfe <l.s.r@web.de>\n---\n apply.c | 7 +++++--\n 1 file changed, 5 insertions(+), 2 deletions(-)\n\ndiff --git a/apply.c b/apply.c\nindex 432837a674..1cfbc2f074 100644\n--- a/apply.c\n+++ b/apply.c\n@@ -4655,8 +4655,11 @@ static int write_out_one_reject(struct apply_state *state, struct patch *patch)\n \t\t\treturn error_errno(_(\"cannot open %s\"), namebuf);\n \t}\n \trej = fdopen(fd, \"w\");\n-\tif (!rej)\n-\t\treturn error_errno(_(\"cannot open %s\"), namebuf);\n+\tif (!rej) {\n+\t\terror_errno(_(\"cannot open %s\"), namebuf);\n+\t\tclose(fd);\n+\t\treturn -1;\n+\t}\n\n \t/* Normal git tools never deal with .rej, so do not pretend\n \t * this is a git patch by saying --git or giving extended\n--\n2.44.0\n"},{"id":"492303","messageId":"xmqqv84vvirr.fsf@gitster.g","threadId":"61272","inReplyTo":"5ba55ee4-94c7-4094-a744-584fc623b391@web.de","subject":"Re: [PATCH] apply: don't leak fd on fdopen() error","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2024-04-05T17:09:12Z","receivedAt":"2024-04-05T17:09:18Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"René Scharfe <l.s.r@web.de> writes:\n\n> Signed-off-by: René Scharfe <l.s.r@web.de>\n> ---\n>  apply.c | 7 +++++--\n>  1 file changed, 5 insertions(+), 2 deletions(-)\n>\n> diff --git a/apply.c b/apply.c\n> index 432837a674..1cfbc2f074 100644\n> --- a/apply.c\n> +++ b/apply.c\n> @@ -4655,8 +4655,11 @@ static int write_out_one_reject(struct apply_state *state, struct patch *patch)\n>  \t\t\treturn error_errno(_(\"cannot open %s\"), namebuf);\n>  \t}\n>  \trej = fdopen(fd, \"w\");\n> -\tif (!rej)\n> -\t\treturn error_errno(_(\"cannot open %s\"), namebuf);\n> +\tif (!rej) {\n> +\t\terror_errno(_(\"cannot open %s\"), namebuf);\n> +\t\tclose(fd);\n> +\t\treturn -1;\n> +\t}\n\nAh, true, this fd is what we created in this function, and we should\nclose it.  Well spotted.\n\nWill queue.\n\n>\n>  \t/* Normal git tools never deal with .rej, so do not pretend\n>  \t * this is a git patch by saying --git or giving extended\n> --\n> 2.44.0\n"}]}