Re: [PATCH 4/6] abbrev_sha1_in_line: don't leak memory
- From
Jeff King <peff@peff.net>
- Date
- Mar 30, 2016, 17:21 UTC
- Message-ID
- <20160330172157.GA19675@sigill.intra.peff.net>
- In-Reply-To
- <xmqqzitfuc7y.fsf@gitster.mtv.corp.google.com>
On Wed, Mar 30, 2016 at 10:06:41AM -0700, Junio C Hamano wrote:
Show 26 quoted lines
> Jeff King <peff@peff.net> writes:
>
> > On Tue, Mar 29, 2016 at 09:30:38PM -0400, Eric Sunshine wrote:
> >
> >> The implementation of strbuf_list_free() is this:
> >>
> >> struct strbuf **s = sbs;
> >> while (*s) {
> >> strbuf_release(*s);
> >> free(*s++);
> >> }
> >> free(sbs);
> >>
> >> which means that wt-status.c is leaking not only 'split', but also
> >> each element of split[], right?
> >
> > Yeah, I didn't notice that, but I think you're right.
>
> Correct.
>
> I suspect that we would be better off in the longer term if
> we made conscious effort to deprecate and eradicate the use
> of strbuf_split* API functions. They are easy to write
> crappy code with, inefficient, and error prone. You would
> rarely need to have N resulting pieces as strbufs to be
> easily manipulatable [*1*].Yeah, I agree that it is a clunky interface, and I would be happy to see it go away. Many callers would be better off with string_list_split(). When I've looked in the past, though, converting some of the callers was somewhat non-trivial.
But in this case...
> The function can be written by not using the "split and then > rebuild" pattern, perhaps like so, and the result is even > easier to read and understand, I would say. A sample rewrite > is attached at the end.
I agree that the function is much simpler without it.
Show 8 quoted lines
> + /* find the second token on the line */ > + cp = strchr(line->buf, ' '); > + if (!cp) > + return; > + cp++; > + ep = strchr(cp, ' '); > + if (!ep) > + return;
Would we ever see "cmd sha1" without a third token? If so, we'd want:
ep = strchrnul(cp, ' ');
-Peff