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

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
Previous: Junio C HamanoNext: Stefan Beller
Message 19 of 24 in “Some cleanups”
  1. 0/6 Some cleanupsStefan Beller, Mar 30, 2016
  2. 1/6 path.c: allocate enough memory for stringStefan Beller, Mar 30, 2016
  3. Junio C HamanoMar 30, 2016
  4. Eric SunshineMar 30, 2016
  5. Stefan BellerMar 30, 2016
  6. Junio C HamanoMar 30, 2016
  7. 2/6 imap-send.c, cram: allocate enough memory for null terminated stringStefan Beller, Mar 30, 2016
  8. Eric SunshineMar 30, 2016
  9. Jeff KingMar 30, 2016
  10. 3/6 notes: don't leak memory in git_config_get_notes_strategyStefan Beller, Mar 30, 2016
  11. Eric SunshineMar 30, 2016
  12. Jeff KingMar 30, 2016
  13. Junio C HamanoMar 30, 2016
  14. 4/6 abbrev_sha1_in_line: don't leak memoryStefan Beller, Mar 30, 2016
  15. Jeff KingMar 30, 2016
  16. Eric SunshineMar 30, 2016
  17. Jeff KingMar 30, 2016
  18. Junio C HamanoMar 30, 2016
  19. Jeff KingMar 30, 2016
  20. 5/6 bundle: don't leak an fd in case of early returnStefan Beller, Mar 30, 2016
  21. Jeff KingMar 30, 2016
  22. Junio C HamanoMar 30, 2016
  23. 6/6 credential-cache, send_request: close fd when doneStefan Beller, Mar 30, 2016
  24. Jeff KingMar 30, 2016

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.