threads / patch / 61272

patchapply: don't leak fd on fdopen() error

Subject: [PATCH] apply: don't leak fd on fdopen() error

## tl;dr

2 messages between Apr 5, 2024 and Apr 5, 2024. Diffs are folded; open one to read it.

replies: 1people: 2as markdown or json

René Scharfe· Apr 5, 2024, 10:58 UTC · lore
Signed-off-by: René Scharfe <l.s.r@web.de>
---
 apply.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
Show changes to apply.c +5 −3
diff --git a/apply.c b/apply.c
index 432837a674..1cfbc2f074 100644
--- a/apply.c
+++ b/apply.c
@@ -4655,8 +4655,11 @@ static int write_out_one_reject(struct apply_state *state, struct patch *patch)
 			return error_errno(_("cannot open %s"), namebuf);
 	}
 	rej = fdopen(fd, "w");
-	if (!rej)
-		return error_errno(_("cannot open %s"), namebuf);
+	if (!rej) {
+		error_errno(_("cannot open %s"), namebuf);
+		close(fd);
+		return -1;
+	}

 	/* Normal git tools never deal with .rej, so do not pretend
 	 * this is a git patch by saying --git or giving extended
--
2.44.0
Junio C Hamano· Apr 5, 2024, 17:09 UTC · re: René Scharfe · lore

Re: [PATCH] apply: don't leak fd on fdopen() error

René Scharfe <l.s.r@web.de> writes:
Show 20 quoted lines
> Signed-off-by: René Scharfe <l.s.r@web.de>
> ---
>  apply.c | 7 +++++--
>  1 file changed, 5 insertions(+), 2 deletions(-)
>
> diff --git a/apply.c b/apply.c
> index 432837a674..1cfbc2f074 100644
> --- a/apply.c
> +++ b/apply.c
> @@ -4655,8 +4655,11 @@ static int write_out_one_reject(struct apply_state *state, struct patch *patch)
>  			return error_errno(_("cannot open %s"), namebuf);
>  	}
>  	rej = fdopen(fd, "w");
> -	if (!rej)
> -		return error_errno(_("cannot open %s"), namebuf);
> +	if (!rej) {
> +		error_errno(_("cannot open %s"), namebuf);
> +		close(fd);
> +		return -1;
> +	}

Ah, true, this fd is what we created in this function, and we should close it. Well spotted.

Will queue.
Show 5 quoted lines
>
>  	/* Normal git tools never deal with .rej, so do not pretend
>  	 * this is a git patch by saying --git or giving extended
> --
> 2.44.0

← back to recent threads