Fix memory leak in function handle_content_type
PATCH 1/1 Fix memory leak in function handle_content_type
From: jinyaoguo <[email protected]>
The function handle_content_type allocates memory for boundary using xmalloc(sizeof(struct strbuf)). If (++mi->content_top >= &mi->content[MAX_BOUNDARIES]) is true, the function returns without freeing boundary.
---
Fix memory leak in function handle_content_type
The function handle_content_type allocates memory for boundary using xmalloc(sizeof(struct strbuf)). If (++mi->content_top >= &mi->content[MAX_BOUNDARIES]) is true, the function returns without freeing boundary.
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1997%2Fmugitya03%2Fmlk-2-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1997/mugitya03/mlk-2-v1
Pull-Request: https://github.com/git/git/pull/1997
mailinfo.c | 3 +++ 1 file changed, 3 insertions(+)
mailinfo.c +3 -0
@@ -266,6 +266,9 @@ static void handle_content_type(struct mailinfo *mi, struct strbuf *line)error ("Too many boundaries to handle" ); mi->input_error =-1 ; mi->content_top = &mi->content[MAX_BOUNDARIES ] -1 ; +strbuf_release (boundary); +free (boundary); + boundary =NULL ;return ; } *(mi->content_top) = boundary;
base-commit: 9edff09aec9b5aaa3d5528129bb279a4d34cf5b3
Signature · 1 line
gitgitgadget
Quoted 5 lines · Alex via GitGitGadget
From: jinyaoguo <[email protected]>
[snip]
Signed-off-by Alex Guo <[email protected]>
Like what was said on another patch[1] the From and Signed-off-by names need to match. I didn’t see a resolution to that?
If forwarding you need to add your signoff after theirs.
[1]: https://lore.kernel.org/git/[email protected]/
Signature · 1 line
Kristoffer Haugsbakk
Hi Kristoffer,
Sorry for that. I’m still getting familiar with the Git patch submission workflow and missed the sign-off requirement. I believe the confusion came from using two different Git identities. I’ve now added individual “Signed-off-by” lines for both accounts.
From b39b1a8176a344a2fa2c46a6d0ffc27a7bfd9edd Mon Sep 17 00:00:00 2001
From: jinyaoguo <[email protected]>
Date: Thu, 12 Jun 2025 18:48:24 -0400
Subject: [PATCH] Fix memory leak in function handle_content_type
The function handle_content_type allocates memory for boundary using xmalloc(sizeof(struct strbuf)). If (++mi->content_top >= &mi->content[MAX_BOUNDARIES]) is true, the function returns without freeing boundary.
--- mailinfo.c | 3 +++ 1 file changed, 3 insertions(+)
mailinfo.c +3 -0
@@ -266,6 +266,9 @@ static void handle_content_type(struct mailinfo *mi, struct strbuf *line)error ("Too many boundaries to handle" ); mi->input_error =-1 ; mi->content_top = &mi->content[MAX_BOUNDARIES ] -1 ; +strbuf_release (boundary); +free (boundary); + boundary =NULL ;return ; } *(mi->content_top) = boundary;
--
2.34.1
________________________________________
From: Kristoffer Haugsbakk <[email protected]>
Sent: Friday, June 13, 2025 12:59
To: Josh Soref <[email protected]>; [email protected] <[email protected]>
Cc: Alex <[email protected]>; Jinyao Guo <[email protected]>
Subject: Re: [PATCH] Fix memory leak in function handle_content_type
[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]
---- External Email: Use caution with attachments, links, or sharing data ----
Quoted 5 lines · Alex via GitGitGadget
From: jinyaoguo <[email protected]>
[snip]
Signed-off-by Alex Guo <[email protected]>
Like what was said on another patch[1] the From and Signed-off-by names need to match. I didn’t see a resolution to that?
If forwarding you need to add your signoff after theirs.
[1]: https://lore.kernel.org/git/[email protected]/
--
Kristoffer Haugsbakk
Quoted 3 lines · Jinyao Guo
... I believe
the confusion came from using two different Git identities. I’ve now added individual “Signed-off-by” lines for both accounts.
Please do not do this, if these two are the same single person.
Instead, pick the one you want to be known as to this community, and use that consistently while working on this project.
Thanks.
Sure. I'll choose one account and use it consistently.
Here is the edited patch:
From 04b286cb2e736c3a53287b6ddf406e704f19fb2e Mon Sep 17 00:00:00 2001
From: jinyaoguo <[email protected]>
Date: Thu, 12 Jun 2025 18:48:24 -0400
Subject: [PATCH] Fix memory leak in function handle_content_type
The function handle_content_type allocates memory for boundary using xmalloc(sizeof(struct strbuf)). If (++mi->content_top >= &mi->content[MAX_BOUNDARIES]) is true, the function returns without freeing boundary.
--- mailinfo.c | 3 +++ 1 file changed, 3 insertions(+)
mailinfo.c +3 -0
@@ -266,6 +266,9 @@ static void handle_content_type(struct mailinfo *mi, struct strbuf *line)error ("Too many boundaries to handle" ); mi->input_error =-1 ; mi->content_top = &mi->content[MAX_BOUNDARIES ] -1 ; +strbuf_release (boundary); +free (boundary); + boundary =NULL ;return ; } *(mi->content_top) = boundary;
Signature · 13 lines
2.34.1 Best, Jinyao ________________________________________ From: Junio C Hamano <[email protected]> Sent: Friday, June 13, 2025 15:06 To: Jinyao Guo <[email protected]> Cc: Kristoffer Haugsbakk <[email protected]>; Josh Soref <[email protected]>; [email protected] <[email protected]>; Alex <[email protected]> Subject: Re: [PATCH] Fix memory leak in function handle_content_type ---- External Email: Use caution with attachments, links, or sharing data ----
Quoted 3 lines · Jinyao Guo
... I believe
the confusion came from using two different Git identities. I’ve now added individual “Signed-off-by” lines for both accounts.
Please do not do this, if these two are the same single person.
Instead, pick the one you want to be known as to this community, and use that consistently while working on this project.
Thanks.
mailinfo.c static void handle_content_type(struct mailinfo *mi, struct strbuf *line) · line 274
Quoted 10 lines · Jinyao Guo
mailinfo.c +3
-0@@ -266,6 +266,9 @@ static void handle_content_type(struct mailinfo *mi, struct strbuf *line)error ("Too many boundaries to handle" ); mi->input_error =-1 ; mi->content_top = &mi->content[MAX_BOUNDARIES ] -1 ; +strbuf_release (boundary); +free (boundary); + boundary =NULL ;return ; } *(mi->content_top) = boundary;
"boundary" is a on-stack local variable. There is no need to assign NULL to it immediately before you return. In the post-context of this hunk, we free it but leave the variable pointing at a random place after that before returning.
Jinyao Guo <[email protected]> writes:
Quoted 36 lines
Sure. I'll choose one account and use it consistently.
Here is the edited patch:
From
04b286cb2e736c3a53287b6ddf406e704f19fb2eMon Sep 17 00:00:00 2001
From: jinyaoguo <[email protected]>
Date: Thu, 12 Jun 2025 18:48:24 -0400
Subject: [PATCH] Fix memory leak in functionhandle_content_typeThe function
handle_content_typeallocates memory for boundary using xmalloc(sizeof(struct strbuf)). If (++mi->content_top >= &mi->content[MAX_BOUNDARIES]) is true, the function returns without freeing boundary.Signed-off-by jinyaoguo <[email protected]>---
mailinfo.c| 3 +++1 file changed, 3 insertions(+)mailinfo.c +3
-0@@ -266,6 +266,9 @@ static void handle_content_type(struct mailinfo *mi, struct strbuf *line)error ("Too many boundaries to handle" ); mi->input_error =-1 ; mi->content_top = &mi->content[MAX_BOUNDARIES ] -1 ; +strbuf_release (boundary); +free (boundary); + boundary =NULL ;return ; } *(mi->content_top) = boundary;Signature · 1 line
2.34.1
May be using goto here would be better. Like:
---
mailinfo.c +2 -1
@@ -266,13 +266,14 @@ static void handle_content_type(struct mailinfo *mi, struct strbuf *line)error ("Too many boundaries to handle" ); mi->input_error =-1 ; mi->content_top = &mi->content[MAX_BOUNDARIES ] -1 ; -return ; + goto out; } *(mi->content_top) = boundary; boundary =NULL ; }slurp_attr (line->buf,"charset=" , &mi->charset); +out:if (boundary) {strbuf_release (boundary);free (boundary);
—
Lidong
Quoted 1 line · Lidong Yan
Subject: Re: [PATCH] Fix memory leak in function
handle_content_type
The subject should probably be something like
Subject: [PATCH] mailinfo.c: plug memory leak in handle_content_type()
Quoted 23 lines
May be using goto here would be better. Like:
---
mailinfo.c +2
-1@@ -266,13 +266,14 @@ static void handle_content_type(struct mailinfo *mi, struct strbuf *line)error ("Too many boundaries to handle" ); mi->input_error =-1 ; mi->content_top = &mi->content[MAX_BOUNDARIES ] -1 ; -return ; + goto out; } *(mi->content_top) = boundary; boundary =NULL ; }slurp_attr (line->buf,"charset=" , &mi->charset); +out:if (boundary) {strbuf_release (boundary);free (boundary);
Quoted 1 line
—
Yup, that one looks good enough.
If we wanted to clean up this code path even further, I think the first clean-up to do is to reconsider the use of "struct strbuf *" (instead of "const char *") in *(mi->content_top). strbuf is a fine and less error-prone mechanism to use while you have to manipulate character strings (like parsing from input line to formulate the boundary string), but once this function computed what was asked by the caller, the computed result (like the boundary string) almost always do not need to be editable. But such a code improvement is totally outside the topic of this patch.