From: lidongyan <502024330056@smail.nju.edu.cn> Date: Tue, 13 May 2025 13:42:29 GMT Subject: Re: [PATCH v4] mailinfo: fix pointential memory leak if `decode_header` failed Message-ID: <9DE4050D-B036-4477-93B0-FF738A6F5C3C@smail.nju.edu.cn> In-Reply-To: Junio C Hamano 写道: > > "Lidong Yan via GitGitGadget" 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`.