mailinfo: fix pointential memory leak if `decode_header` failed

In mailinfo.c:decode_header, if convert_to_utf8 failed, the strbuf stored in dec will leak. Simply add strbuf_release and free(dec) will solve this problem. Signed-off-by: Lidong Yan <502024330056@smail.nju.edu.cn> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Lidong Yan committed May 13, 2025 at 02:49 UTC 56f1cd10f48a5f630633a0e65696917e6f70fdd9
1 file changed +21 -21
mailinfo.c
+21 -21
@@ -381,12 +381,12 @@ static int is_format_patch_separator(const char *line, int len)
381 return !memcmp(SAMPLE + (cp - line), cp, strlen(SAMPLE) - (cp - line));
382 }
383
384 -static struct strbuf *decode_q_segment(const struct strbuf *q_seg, int rfc2047)
384 +static int decode_q_segment(struct strbuf *out, const struct strbuf *q_seg,
385 + int rfc2047)
386 {
387 const char *in = q_seg->buf;
388 int c;
388 - struct strbuf *out = xmalloc(sizeof(struct strbuf));
389 - strbuf_init(out, q_seg->len);
389 + strbuf_grow(out, q_seg->len);
390
391 while ((c = *in++) != 0) {
392 if (c == '=') {
@@ -405,16 +405,15 @@ static struct strbuf *decode_q_segment(const struct strbuf *q_seg, int rfc2047)
405 c = 0x20;
406 strbuf_addch(out, c);
407 }
408 - return out;
408 + return 0;
409 }
410
411 -static struct strbuf *decode_b_segment(const struct strbuf *b_seg)
411 +static int decode_b_segment(struct strbuf *out, const struct strbuf *b_seg)
412 {
413 /* Decode in..ep, possibly in-place to ot */
414 int c, pos = 0, acc = 0;
415 const char *in = b_seg->buf;
416 - struct strbuf *out = xmalloc(sizeof(struct strbuf));
417 - strbuf_init(out, b_seg->len);
416 + strbuf_grow(out, b_seg->len);
417
418 while ((c = *in++) != 0) {
419 if (c == '+')
@@ -447,7 +446,7 @@ static struct strbuf *decode_b_segment(const struct strbuf *b_seg)
446 break;
447 }
448 }
450 - return out;
449 + return 0;
450 }
451
452 static int convert_to_utf8(struct mailinfo *mi,
@@ -475,7 +474,7 @@ static int convert_to_utf8(struct mailinfo *mi,
474 static void decode_header(struct mailinfo *mi, struct strbuf *it)
475 {
476 char *in, *ep, *cp;
478 - struct strbuf outbuf = STRBUF_INIT, *dec;
477 + struct strbuf outbuf = STRBUF_INIT, dec = STRBUF_INIT;
478 struct strbuf charset_q = STRBUF_INIT, piecebuf = STRBUF_INIT;
479 int found_error = 1; /* pessimism */
480
@@ -530,18 +529,19 @@ static void decode_header(struct mailinfo *mi, struct strbuf *it)
529 default:
530 goto release_return;
531 case 'b':
533 - dec = decode_b_segment(&piecebuf);
532 + if ((found_error = decode_b_segment(&dec, &piecebuf)))
533 + goto release_return;
534 break;
535 case 'q':
536 - dec = decode_q_segment(&piecebuf, 1);
536 + if ((found_error = decode_q_segment(&dec, &piecebuf, 1)))
537 + goto release_return;
538 break;
539 }
539 - if (convert_to_utf8(mi, dec, charset_q.buf))
540 + if (convert_to_utf8(mi, &dec, charset_q.buf))
541 goto release_return;
542
542 - strbuf_addbuf(&outbuf, dec);
543 - strbuf_release(dec);
544 - free(dec);
543 + strbuf_addbuf(&outbuf, &dec);
544 + strbuf_release(&dec);
545 in = ep + 2;
546 }
547 strbuf_addstr(&outbuf, in);
@@ -552,6 +552,7 @@ release_return:
552 strbuf_release(&outbuf);
553 strbuf_release(&charset_q);
554 strbuf_release(&piecebuf);
555 + strbuf_release(&dec);
556
557 if (found_error)
558 mi->input_error = -1;
@@ -634,23 +635,22 @@ static int is_inbody_header(const struct mailinfo *mi,
635
636 static void decode_transfer_encoding(struct mailinfo *mi, struct strbuf *line)
637 {
637 - struct strbuf *ret;
638 + struct strbuf ret = STRBUF_INIT;
639
640 switch (mi->transfer_encoding) {
641 case TE_QP:
641 - ret = decode_q_segment(line, 0);
642 + decode_q_segment(&ret, line, 0);
643 break;
644 case TE_BASE64:
644 - ret = decode_b_segment(line);
645 + decode_b_segment(&ret, line);
646 break;
647 case TE_DONTCARE:
648 default:
649 return;
650 }
651 strbuf_reset(line);
651 - strbuf_addbuf(line, ret);
652 - strbuf_release(ret);
653 - free(ret);
652 + strbuf_addbuf(line, &ret);
653 + strbuf_release(&ret);
654 }
655
656 static inline int patchbreak(const struct strbuf *line)