From: Junio C Hamano <gitster@pobox.com>
To: "Lidong Yan via GitGitGadget" <gitgitgadget@gmail.com>
Cc: git@vger.kernel.org, Lidong Yan <502024330056@smail.nju.edu.cn>
Subject: Re: [PATCH] decode_header: fix pointential memory leak if decode_header failed
Date: Thu, 08 May 2025 15:16:07 -0700 [thread overview]
Message-ID: <xmqqh61u4ul4.fsf@gitster.g> (raw)
In-Reply-To: <pull.1956.git.git.1746711521614.gitgitgadget@gmail.com> (Lidong Yan via GitGitGadget's message of "Thu, 08 May 2025 13:38:41 +0000")
"Lidong Yan via GitGitGadget" <gitgitgadget@gmail.com> writes:
> From: Lidong Yan <502024330056@smail.nju.edu.cn>
>
> In mailinfo.c line 539, if convert_to_utf8 failed, the strbuf stored
> in dec will leak. Simply add strbuf_release and free(dec) will solve
> this problem.
We try to write our proposed log messages so that readers can
understand the idea behind the change without having to look at the
patch. Even to those who are intimately familiar with this area of
the code base, an exact line number reference rarely add any useful
information. Something like "In mailinfo.c:decode_header()" would
help them better than "In mailinfo.c line 539".
> Signed-off-by: Lidong Yan <502024330056@smail.nju.edu.cn>
> ---
> decode_header: fix pointential memory leak if decode_header failed
>
> In mailinfo.c line 539, if convert_to_utf8 failed, the strbuf stored in
> dec will leak. Simply add strbuf_release and free(dec) will solve this
> problem.
Just FYI, here is a space to describe what would not have to go into
the proposed log message; there is no need to duplicate what you
already said in the log message above.
> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1956%2Fbrandb97%2Ffix-mailinfo-decode-header-leak-v1
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1956/brandb97/fix-mailinfo-decode-header-leak-v1
> Pull-Request: https://github.com/git/git/pull/1956
>
> mailinfo.c | 5 ++++-
> 1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/mailinfo.c b/mailinfo.c
> index 7b001fa5dbd..7a54471a481 100644
> --- a/mailinfo.c
> +++ b/mailinfo.c
> @@ -536,8 +536,11 @@ static void decode_header(struct mailinfo *mi, struct strbuf *it)
> dec = decode_q_segment(&piecebuf, 1);
> break;
> }
> - if (convert_to_utf8(mi, dec, charset_q.buf))
> + if (convert_to_utf8(mi, dec, charset_q.buf)) {
> + strbuf_release(dec);
> + free(dec);
OK, this fix is obviously correct.
A nicer fix for longer-term may however be to fix the calling
convention for decode_?_segment() functions, so that they take a
caller-prepared strbuf as a parameter and fill it (and signal an
error by returning -1, a success by returning 0). There is no way
for them to signal errors they detect (if we do not count the usual
form of doing so by returning NULL, which this caller is not
expecting) with the current calling convention.
We'd still need to release the data in the strbuf "dec" even if we
did so, but the strbuf would be on stack so there is no need to
free().
> goto release_return;
> + }
>
> strbuf_addbuf(&outbuf, dec);
> strbuf_release(dec);
>
> base-commit: 6f84262c44a89851c3ae5a6e4c1a9d06b2068d75
Thanks.
next prev parent reply other threads:[~2025-05-08 22:16 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-08 13:38 [PATCH] decode_header: fix pointential memory leak if decode_header failed Lidong Yan via GitGitGadget
2025-05-08 22:16 ` Junio C Hamano [this message]
2025-05-09 1:51 ` lidongyan
2025-05-11 16:14 ` [PATCH v2] mailinfo: fix pointential memory leak if `decode_header` failed Lidong Yan via GitGitGadget
2025-05-12 14:12 ` Junio C Hamano
2025-05-12 16:17 ` [PATCH v3] " Lidong Yan via GitGitGadget
2025-05-12 19:32 ` Junio C Hamano
2025-05-13 2:49 ` [PATCH v4] " Lidong Yan via GitGitGadget
2025-05-13 13:34 ` Junio C Hamano
2025-05-13 13:42 ` lidongyan
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=xmqqh61u4ul4.fsf@gitster.g \
--to=gitster@pobox.com \
--cc=502024330056@smail.nju.edu.cn \
--cc=git@vger.kernel.org \
--cc=gitgitgadget@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).