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

[PATCH v2] dropping manual malloc calculations

From
Jeff King <peff@peff.net>
Date
Jun 19, 2014, 21:16 UTC
Message-ID
<20140619211659.GA32412@sigill.intra.peff.net>
In-Reply-To
<xmqq38f0j13u.fsf@gitster.dls.corp.google.com>
On Thu, Jun 19, 2014 at 09:49:41AM -0700, Junio C Hamano wrote:
> If we twist the logic behind the 'mkpathdup' name a little bit,
> perhaps we can call it sprintf_dup or something.  That is, "sprintf
> wants a fixed preallocated piece of memory to print into, and we
> relieve the callers of having to do so", perhaps?

Right, that is the "dup" I was thinking of. Anyway, I think René's "xstrfmt" is my favorite of the suggestions, and you seemed to like it, too. Here is a re-roll using that name.

The first two patches are the same except for the name change. The rest are more applications of it (and other techniques) found by grepping for "malloc.*strlen". The diffstat is very encouraging, and I think the results are much more readable (in addition to being more obviously correct). The last two also fix real "bugs", but I doubt either is a problem in practice.

    builtin/apply.c         |  4 +--
    builtin/fetch.c         |  9 ++----
    builtin/fmt-merge-msg.c |  7 ++---
    builtin/name-rev.c      |  5 +---
    builtin/receive-pack.c  |  5 +---
    builtin/show-branch.c   | 10 +++----
    environment.c           | 12 +++-----
    http-push.c             | 24 +++++-----------
    http-walker.c           |  6 ++--
    match-trees.c           |  9 ++----
    merge-recursive.c       | 53 ++++++++++++++++++++---------------
    merge.c                 | 42 +++++++++------------------
    remote.c                |  6 +---
    sequencer.c             |  9 ++----
    sha1_name.c             |  5 +---
    shell.c                 |  6 +---
    strbuf.c                | 19 +++++++++++++
    strbuf.h                |  9 ++++++
    unpack-trees.c          | 17 ++++-------
    walker.c                | 18 ++++++------
    20 files changed, 117 insertions(+), 158 deletions(-)
  [01/10]: strbuf: add xstrfmt helper
  [02/10]: use xstrfmt in favor of manual size calculations
  [03/10]: use xstrdup instead of xmalloc + strcpy
  [04/10]: use xstrfmt to replace xmalloc + sprintf
  [05/10]: use xstrfmt to replace xmalloc + strcpy/strcat
  [06/10]: setup_git_env: use git_pathdup instead of xmalloc + sprintf
  [07/10]: sequencer: use argv_array_pushf
  [08/10]: merge: use argv_array when spawning merge strategy
  [09/10]: walker_fetch: fix minor memory leak
  [10/10]: unique_path: fix unlikely heap overflow
Previous: Junio C HamanoNext: Jeff King
Message 6 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.