* [PATCH] builtin/am: fix memory leak in `split_mail_stgit_series`
@ 2025-05-12 2:07 Lidong Yan via GitGitGadget
2025-05-12 8:10 ` Patrick Steinhardt
2025-05-12 14:02 ` Junio C Hamano
0 siblings, 2 replies; 3+ messages in thread
From: Lidong Yan via GitGitGadget @ 2025-05-12 2:07 UTC (permalink / raw)
To: git; +Cc: Lidong Yan, Lidong Yan
From: Lidong Yan <502024330056@smail.nju.edu.cn>
In builtin/am.c:split_mail_stgit_series, if `fopen` failed,
`series_dir_buf` allocated by `xstrdup` will leak. Add `free` in
`!fp` if branch will prevent the leak.
Signed-off-by: Lidong Yan <502024330056@smail.nju.edu.cn>
---
builtin/am: fix memory leak in split_mail_stgit_series
In builtin/am.c:split_mail_stgit_series, if fopen failed, series_dir_buf
allocated by xstrdup will leak. Add free in !fp if branch will prevent
the leak.
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1961%2Fbrandb97%2Ffix-am-leak-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1961/brandb97/fix-am-leak-v1
Pull-Request: https://github.com/git/git/pull/1961
builtin/am.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/builtin/am.c b/builtin/am.c
index 4afb519830f..e32a3b4c973 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -850,8 +850,10 @@ static int split_mail_stgit_series(struct am_state *state, const char **paths,
series_dir = dirname(series_dir_buf);
fp = fopen(*paths, "r");
- if (!fp)
+ if (!fp) {
+ free(series_dir_buf);
return error_errno(_("could not open '%s' for reading"), *paths);
+ }
while (!strbuf_getline_lf(&sb, fp)) {
if (*sb.buf == '#')
base-commit: 6f84262c44a89851c3ae5a6e4c1a9d06b2068d75
--
gitgitgadget
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] builtin/am: fix memory leak in `split_mail_stgit_series`
2025-05-12 2:07 [PATCH] builtin/am: fix memory leak in `split_mail_stgit_series` Lidong Yan via GitGitGadget
@ 2025-05-12 8:10 ` Patrick Steinhardt
2025-05-12 14:02 ` Junio C Hamano
1 sibling, 0 replies; 3+ messages in thread
From: Patrick Steinhardt @ 2025-05-12 8:10 UTC (permalink / raw)
To: Lidong Yan via GitGitGadget; +Cc: git, Lidong Yan
On Mon, May 12, 2025 at 02:07:27AM +0000, Lidong Yan via GitGitGadget wrote:
> From: Lidong Yan <502024330056@smail.nju.edu.cn>
>
> In builtin/am.c:split_mail_stgit_series, if `fopen` failed,
> `series_dir_buf` allocated by `xstrdup` will leak. Add `free` in
> `!fp` if branch will prevent the leak.
>
> Signed-off-by: Lidong Yan <502024330056@smail.nju.edu.cn>
This fix looks obviously correct to me. Thanks!
Patrick
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] builtin/am: fix memory leak in `split_mail_stgit_series`
2025-05-12 2:07 [PATCH] builtin/am: fix memory leak in `split_mail_stgit_series` Lidong Yan via GitGitGadget
2025-05-12 8:10 ` Patrick Steinhardt
@ 2025-05-12 14:02 ` Junio C Hamano
1 sibling, 0 replies; 3+ messages in thread
From: Junio C Hamano @ 2025-05-12 14:02 UTC (permalink / raw)
To: Lidong Yan via GitGitGadget; +Cc: git, Lidong Yan
"Lidong Yan via GitGitGadget" <gitgitgadget@gmail.com> writes:
> From: Lidong Yan <502024330056@smail.nju.edu.cn>
>
> In builtin/am.c:split_mail_stgit_series, if `fopen` failed,
> `series_dir_buf` allocated by `xstrdup` will leak. Add `free` in
> `!fp` if branch will prevent the leak.
>
> diff --git a/builtin/am.c b/builtin/am.c
> index 4afb519830f..e32a3b4c973 100644
> --- a/builtin/am.c
> +++ b/builtin/am.c
> @@ -850,8 +850,10 @@ static int split_mail_stgit_series(struct am_state *state, const char **paths,
> series_dir = dirname(series_dir_buf);
>
> fp = fopen(*paths, "r");
> - if (!fp)
> + if (!fp) {
> + free(series_dir_buf);
> return error_errno(_("could not open '%s' for reading"), *paths);
> + }
>
> while (!strbuf_getline_lf(&sb, fp)) {
> if (*sb.buf == '#')
>
> base-commit: 6f84262c44a89851c3ae5a6e4c1a9d06b2068d75
Obviously correct; will queue. Thanks.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-05-12 14:02 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-05-12 2:07 [PATCH] builtin/am: fix memory leak in `split_mail_stgit_series` Lidong Yan via GitGitGadget
2025-05-12 8:10 ` Patrick Steinhardt
2025-05-12 14:02 ` Junio C Hamano
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).