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

Re: [PATCH 1/2] strbuf: add xstrdup_fmt helper

From
Jeff King <peff@peff.net>
Date
Jun 19, 2014, 09:05 UTC
Message-ID
<20140619090532.GB1009@sigill.intra.peff.net>
In-Reply-To
<xmqq7g4dj1cn.fsf@gitster.dls.corp.google.com>
On Wed, Jun 18, 2014 at 03:32:08PM -0700, Junio C Hamano wrote:
Show 9 quoted lines
> >   str = xstrdup_fmt(fmt, some, args);
> > ---
> > I'm open to suggestions on the name. This really is the same thing
> > conceptually as the GNU asprintf(), but the interface is different (that
> > function takes a pointer-to-pointer as an out-parameter, and returns the
> > number of characters return).
> 
> Naming it with anything "dup" certainly feels wrong.  The returned
> string is not a duplicate of anything.

I was somewhat inspired by "mkpathdup" and friends. It is quite simialr to mkpathdup, except that it is not limited to paths (and does not do any path-specific munging). I agree something with "printf" in the name is probably more descriptive, though.

Show 7 quoted lines
> I wonder if our callers can instead use asprintf(3) with its
> slightly more quirky API (and then we supply compat/asprintf.c for
> non-GNU platforms).  Right now we only have three call sites, but if
> we anticipate that "printf-like format into an allocated piece of
> memory" will prove be generally useful in our code base, following
> an API that other people already have established may give our
> developers one less thing that they have to learn.

I considered that, but I do find asprintf's interface unnecessarily annoying (you can't return a value directly, and you run afoul of const issues when passing pointers to pointers). As you noted, it's not _too_ bad, but we really get nothing from the integer return type. AFAICT, it helps with:

  1. You know how many characters are in the string. If you cared about
     that here, you would just use a strbuf directly, which is much more
     flexible.
  2. The error handling is different. But our x-variant would just die()
     on errors anyway, so we do not care.

So modeling after asprintf feels like carrying around unnecessary baggage (and I am not convinced asprintf is in wide enough use, nor that the function is complicated enough to care about developer familiarity). Part of me is tempted to call it xasprintf anyway, and use our own interface. GNU does not get to squat on that useful name. ;)

That is probably unnecessarily confusing, though.
> As usual, I would expect we would have xasprintf wrapper around it
> to die instead of returning -1 upon allocation failure.

Would it be crazy to just call it xprintf? By our current scheme that would be "a wrapper for printf", which means it is potentially confusing.

Literally any other unused letter would be fine. dprintf for detach? I dunno.

-Peff
Previous: Junio C HamanoNext: Junio C Hamano
Message 4 of 27 in “dropping manual malloc calculations”
  1. 0/2 dropping manual malloc calculationsJeff King, Jun 18, 2014
  2. 1/2 strbuf: add xstrdup_fmt helperJeff King, Jun 18, 2014
  3. Junio C HamanoJun 18, 2014
  4. Jeff KingJun 19, 2014
  5. Junio C HamanoJun 19, 2014
  6. dropping manual malloc calculationsJeff King, Jun 19, 2014
  7. 01/10 strbuf: add xstrfmt helperJeff King, Jun 19, 2014
  8. 02/10 use xstrfmt in favor of manual size calculationsJeff King, Jun 19, 2014
  9. 03/10 use xstrdup instead of xmalloc + strcpyJeff King, Jun 19, 2014
  10. 04/10 use xstrfmt to replace xmalloc + sprintfJeff King, Jun 19, 2014
  11. 05/10 use xstrfmt to replace xmalloc + strcpy/strcatJeff King, Jun 19, 2014
  12. 06/10 setup_git_env: use git_pathdup instead of xmalloc + sprintfJeff King, Jun 19, 2014
  13. Eric SunshineJun 23, 2014
  14. Junio C HamanoJun 23, 2014
  15. Duy NguyenJun 24, 2014
  16. Duy NguyenJun 24, 2014
  17. Jeff KingJun 24, 2014
  18. Duy NguyenJun 25, 2014
  19. Junio C HamanoJun 25, 2014
  20. Jeff KingJun 25, 2014
  21. Junio C HamanoJun 25, 2014
  22. 07/10 sequencer: use argv_array_pushfJeff King, Jun 19, 2014
  23. 08/10 merge: use argv_array when spawning merge strategyJeff King, Jun 19, 2014
  24. 09/10 walker_fetch: fix minor memory leakJeff King, Jun 19, 2014
  25. 10/10 unique_path: fix unlikely heap overflowJeff King, Jun 19, 2014
  26. René ScharfeJun 19, 2014
  27. 2/2 use xstrdup_fmt in favor of manual size calculationsJeff King, Jun 18, 2014

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.