Re: [PATCH v4] mailinfo: fix pointential memory leak if `decode_header` failed
- From
lidongyan <502024330056@smail.nju.edu.cn>
- Date
- May 13, 2025, 13:42 UTC
- Message-ID
- <9DE4050D-B036-4477-93B0-FF738A6F5C3C@smail.nju.edu.cn>
- In-Reply-To
- <xmqq4ixoiqhu.fsf@gitster.g>
Junio C Hamano <gitster@pobox.com> 写道:
Show 18 quoted lines
>
> "Lidong Yan via GitGitGadget" <gitgitgadget@gmail.com> writes:
>
>> -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);
>> + strbuf_grow(out, q_seg->len);
>
> Call to grow(), while it does not hurt correctness, would not be
> necessary here, but let's take this code as-is. As the result of Q
> encoding (and B encoding as well) always is longer than the decoded
> result, strbuf_grow(out, q_seg->len) would always be over-allocating,
> but it would not hurt too much.Thanks for your advice. Though I think `strbuf_init(out, q_seg->len)` also call `strbuf_grow` to reserve `q_seg->len` size memory for `out`. `strbuf_grow` here prevent multiple realloc operations that might occur when calling `strbuf_addch`.