git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v3] mailinfo: fix pointential memory leak if `decode_header` failed

From
Junio C Hamano <gitster@pobox.com>
Date
May 12, 2025, 19:32 UTC
Message-ID
<xmqqbjrxsk07.fsf@gitster.g>
In-Reply-To
<pull.1956.v3.git.git.1747066627330.gitgitgadget@gmail.com>
"Lidong Yan via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 8 quoted lines
> -static struct strbuf *decode_q_segment(const struct strbuf *q_seg, int rfc2047)
> +static int decode_q_segment(struct strbuf *out, const struct strbuf *q_seg,
> +			    int rfc2047)
>  {
>  	const char *in = q_seg->buf;
>  	int c;
> -	struct strbuf *out = xmalloc(sizeof(struct strbuf));
>  	strbuf_init(out, q_seg->len);
This strbuf_init() is still here?
Show 7 quoted lines
> +static int decode_b_segment(struct strbuf *out, const struct strbuf *b_seg)
>  {
>  	/* Decode in..ep, possibly in-place to ot */
>  	int c, pos = 0, acc = 0;
>  	const char *in = b_seg->buf;
> -	struct strbuf *out = xmalloc(sizeof(struct strbuf));
>  	strbuf_init(out, b_seg->len);
Ditto.
Show 6 quoted lines
> @@ -475,7 +474,7 @@ static int convert_to_utf8(struct mailinfo *mi,
>  static void decode_header(struct mailinfo *mi, struct strbuf *it)
>  {
>  	char *in, *ep, *cp;
> -	struct strbuf outbuf = STRBUF_INIT, *dec;
> +	struct strbuf outbuf = STRBUF_INIT, dec = STRBUF_INIT;
Initializing this at the caller side is good.
Show 18 quoted lines
> @@ -530,18 +529,20 @@ static void decode_header(struct mailinfo *mi, struct strbuf *it)
>  		default:
>  			goto release_return;
>  		case 'b':
> -			dec = decode_b_segment(&piecebuf);
> +			if ((found_error = decode_b_segment(&dec, &piecebuf)))
> +				goto release_return;
>  			break;
>  		case 'q':
> -			dec = decode_q_segment(&piecebuf, 1);
> +			if ((found_error = decode_q_segment(&dec, &piecebuf, 1)))
> +				goto release_return;
>  			break;
>  		}
> -		if (convert_to_utf8(mi, dec, charset_q.buf))
> +		if (convert_to_utf8(mi, &dec, charset_q.buf)) {
>  			goto release_return;
> +		}
Don't enclose a single statement block inside {braces}.
Thanks.
Previous: Lidong Yan via GitGitGadgetNext: Lidong Yan via GitGitGadget
Message 7 of 10 in “decode_header: fix pointential memory leak if decode_header failed”
  1. decode_header: fix pointential memory leak if decode_header failedLidong Yan via GitGitGadget, May 8, 2025
  2. Junio C HamanoMay 8, 2025
  3. lidongyanMay 9, 2025
  4. mailinfo: fix pointential memory leak if `decode_header` failedLidong Yan via GitGitGadget, May 11, 2025
  5. Junio C HamanoMay 12, 2025
  6. mailinfo: fix pointential memory leak if `decode_header` failedLidong Yan via GitGitGadget, May 12, 2025
  7. Junio C HamanoMay 12, 2025
  8. mailinfo: fix pointential memory leak if `decode_header` failedLidong Yan via GitGitGadget, May 13, 2025
  9. Junio C HamanoMay 13, 2025
  10. lidongyanMay 13, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.