Skip to contentSkip to conversation

builtin/am: fix memory leak in `split_mail_stgit_series`

PATCH In review Lidong Yan via GitGitGadget 3 participants 3 messages May 12, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 builtin/am: fix memory leak in `split_mail_stgit_series`

From: Lidong Yan <[email protected]>

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.

---
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(-)
builtin/am.c +3 -1
index 4afb519830f..e32a3b4c973 100644
@@ -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

Signature · 1 line
gitgitgadget
#1 lore raw
Quoted 7 lines · Lidong Yan via GitGitGadget

From: Lidong Yan <[email protected]>

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.

This fix looks obviously correct to me. Thanks!

Patrick

#2 lore raw
In reply to Lidong Yan via GitGitGadget
Quoted 23 lines · Lidong Yan via GitGitGadget

From: Lidong Yan <[email protected]>

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.

builtin/am.c +3 -1
index 4afb519830f..e32a3b4c973 100644
@@ -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 == '#')
Quoted 1 line

base-commit: 6f84262c44a89851c3ae5a6e4c1a9d06b2068d75

Obviously correct; will queue. Thanks.