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

Re: [PATCH] fmt-merge-msg: avoid leaking strbuf in shortlog()

From
Jeff King <peff@peff.net>
Date
Dec 8, 2017, 21:17 UTC
Message-ID
<20171208211706.GB7355@sigill.intra.peff.net>
In-Reply-To
<1654a696-73d5-c9ef-0fc2-bd82aaf2cabb@web.de>
On Fri, Dec 08, 2017 at 06:29:34PM +0100, René Scharfe wrote:
Show 18 quoted lines
> > By the way, I think there's another quite subtle leak in this function.
> > We do this:
> > 
> >    format_commit_message(commit, "%s", &sb, &ctx);
> >    strbuf_ltrim(&sb);
> > 
> > and then only use "sb" if sb.len is non-zero. But we may have actually
> > allocated to create our zero-length string (e.g., if we had a strbuf
> > full of spaces and trimmed them all off). Since we reuse "sb" over and
> > over as we loop, this will actually only leak once for the whole loop,
> > not once per iteration. So it's probably not a big deal, but writing it
> > with the explicit reset/release pattern fixes that (and is more
> > idiomatic for our code base, I think).
> 
> It's subtle, but I think it's not leaking, at least not in your example
> case (and I can't think of another way).  IIUC format_subject(), which
> handles the "%s" part, doesn't touch sb if the subject is made up only
> of whitespace.

Yeah, I suspected that may be the case. But IMHO it is a poor use of strbufs if you have to dig that far to see whether the code leaks or not. The whole point of strbufs is to make string handling and memory ownership more obviously correct.

Just skimming the history, I think it's mostly an artifact of the function was slowly converted over the years.

-Peff
Previous: Jeff King
Message 16 of 16 in “fmt-merge-msg: avoid leaking strbuf in shortlog()”
  1. fmt-merge-msg: avoid leaking strbuf in shortlog()René Scharfe, Dec 7, 2017
  2. Jeff KingDec 7, 2017
  3. René ScharfeDec 8, 2017
  4. Junio C HamanoDec 8, 2017
  5. René ScharfeDec 8, 2017
  6. Jeff KingDec 8, 2017
  7. Junio C HamanoDec 7, 2017
  8. Jeff KingDec 8, 2017
  9. René ScharfeDec 8, 2017
  10. Junio C HamanoDec 8, 2017
  11. Jeff KingDec 8, 2017
  12. René ScharfeDec 18, 2017
  13. Jeff KingDec 19, 2017
  14. René ScharfeDec 19, 2017
  15. Jeff KingDec 20, 2017
  16. Jeff KingDec 8, 2017

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.