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

[PATCH v2 0/68] war on sprintf

From
Jeff King <peff@peff.net>
Date
Sep 24, 2015, 21:02 UTC
Message-ID
<20150924210225.GA23624@sigill.intra.peff.net>
This is a revised version of the series I sent earlier[1].

For those just joining us, the goal is to remove sprintf, strcpy, etc, to make it easier to audit the code base for buffer overflows. I've been addressing review comments for individual patches as the discussion progressed, so there's nothing here that hasn't been on the list (and I think I've addressed all of the comments from the first round of review).

Here's a list of the commits, with an overview[2] of the changes from v1 (patches without comment are the same as in v1):

  [01/68]: show-branch: avoid segfault with --reflog of unborn branch
  [02/68]: mailsplit: fix FILE* leak in split_maildir
  [03/68]: archive-tar: fix minor indentation violation
  [04/68]: fsck: don't fsck alternates for connectivity-only check
  [05/68]: add xsnprintf helper function
  [06/68]: add git_path_buf helper function
  [07/68]: strbuf: make strbuf_complete_line more generic
    Docstring now makes clear the behavior when the strbuf is empty.
  [08/68]: add reentrant variants of sha1_to_hex and find_unique_abbrev
    These are now called sha1_to_hex_r, etc. The re-entrancy conditions
    are documented.
  [09/68]: fsck: use strbuf to generate alternate directories
  [10/68]: mailsplit: make PATH_MAX buffers dynamic
    Use xstrfmt consistently instead of sometimes using a strbuf.
  [11/68]: trace: use strbuf for quote_crnl output
    Further cleanup of "p2" variable in quote_crnl.
  [12/68]: progress: store throughput display in a strbuf
  [13/68]: test-dump-cache-tree: avoid overflow of cache-tree name
  [14/68]: compat/inet_ntop: fix off-by-one in inet_ntop4
  [15/68]: convert trivial sprintf / strcpy calls to xsnprintf
    Prefer "fixed-size" to "static" for clarity in commit message.
  [16/68]: archive-tar: use xsnprintf for trivial formatting
  [17/68]: use xsnprintf for generating git object headers
  [18/68]: find_short_object_filename: convert sprintf to xsnprintf
  [19/68]: stop_progress_msg: convert sprintf to xsnprintf
    Ditto on "fixed-size" versus "static".
  [20/68]: compat/hstrerror: convert sprintf to snprintf
  [21/68]: grep: use xsnprintf to format failure message
  [22/68]: entry.c: convert strcpy to xsnprintf
  [23/68]: add_packed_git: convert strcpy into xsnprintf
    Drop useless comment. Add comment on magic strlen().
  [24/68]: http-push: replace strcat with xsnprintf
  [25/68]: receive-pack: convert strncpy to xsnprintf
  [26/68]: replace trivial malloc + sprintf / strcpy calls with xstrfmt
    Include NUL in base64 of imap-send cram response. My guess is this
    is a bug in what we send that is overlooked by most servers, but I'd
    prefer to be conservative here and keep the behavior the same.
  [27/68]: config: use xstrfmt in normalize_value
  [28/68]: fetch: replace static buffer with xstrfmt
  [29/68]: use strip_suffix and xstrfmt to replace suffix
  [30/68]: ref-filter: drop sprintf and strcpy calls
  [31/68]: help: drop prepend function in favor of xstrfmt
  [32/68]: mailmap: replace strcpy with xstrdup
  [33/68]: read_branches_file: simplify string handling
    This goes much further than the original in cleaning up the use of a
    static buffer. From what I sent earlier during review, I dropped
    strbuf_read_file in favor of strbuf_getline, to keep the behavior
    identical to the original.
  [34/68]: read_remotes_file: simplify string handling
    New in this iteration; cleanups to match those in 33/68.
  [35/68]: resolve_ref: use strbufs for internal buffers
  [36/68]: upload-archive: convert sprintf to strbuf
  [37/68]: remote-ext: simplify git pkt-line generation
    The v1 of this patch was totally buggy. This is a rewrite to use
    packet_write(), which has several advantages, and includes tests.
  [38/68]: http-push: use strbuf instead of fwrite_buffer
  [39/68]: http-walker: store url in a strbuf
  [40/68]: sha1_get_pack_name: use a strbuf
  [41/68]: init: use strbufs to store paths
  [42/68]: apply: convert root string to strbuf
  [43/68]: transport: use strbufs for status table "quickref" strings
  [44/68]: merge-recursive: convert malloc / strcpy to strbuf
  [45/68]: enter_repo: convert fixed-size buffers to strbufs
  [46/68]: remove_leading_path: use a strbuf for internal storage
  [47/68]: write_loose_object: convert to strbuf
    Clarify comment on subtle mkstemp behavior.
  [48/68]: diagnose_invalid_index_path: use strbuf to avoid strcpy/strcat
  [49/68]: fetch-pack: use argv_array for index-pack / unpack-objects
  [50/68]: http-push: use an argv_array for setup_revisions
  [51/68]: stat_tracking_info: convert to argv_array
  [52/68]: daemon: use cld->env_array when re-spawning
  [53/68]: use sha1_to_hex_r() instead of strcpy
    Use "_r" versions to match change in patch 8.
  [54/68]: drop strcpy in favor of raw sha1_to_hex
    More history in the commit message, courtesy of Eric.
  [55/68]: color: add overflow checks for parsing colors
    Be more careful with OUT() macro.
  [56/68]: use alloc_ref rather than hand-allocating "struct ref"
  [57/68]: avoid sprintf and strcpy with flex arrays
    Trivial typo-fix in comment.
  [58/68]: receive-pack: simplify keep_arg computation
    Another s/static/fixed-size/.
  [59/68]: help: clean up kfmclient munging
  [60/68]: prefer memcpy to strcpy
  [61/68]: color: add color_set helper for copying raw colors
  [62/68]: notes: document length of fanout path with a constant
  [63/68]: convert strncpy to memcpy
  [64/68]: fsck: drop inode-sorting code
  [65/68]: Makefile: drop D_INO_IN_DIRENT build knob
  [66/68]: fsck: use for_each_loose_file_in_objdir
  [67/68]: use strbuf_complete to conditionally append slash
    Drop bogus conversion from imap-send.
  [68/68]: name-rev: use strip_suffix to avoid magic numbers

Hopefully that makes reviewing v2 a little less painful for those who already slogged through v1. Thanks for your patience. :)

-Peff
[1] Gmane seems down, but once it's back up you should be able to get to
    v1 at: http://mid.gmane.org/20150915152125.GA27504@sigill.intra.peff.net
[2] Keeping track of the tweaks to this many patches would have been
    near impossible without Thomas Rast's git-tbdiff tool:
      https://github.com/trast/tbdiff
    I highly recommend it for people handling multiple versions of a
    long series.
Next: Jeff King
Message 1 of 93 in “war on sprintf”
  1. 0/68 war on sprintfJeff King, Sep 24, 2015
  2. 01/68 show-branch: avoid segfault with --reflog of unborn branchJeff King, Sep 24, 2015
  3. 02/68 mailsplit: fix FILE* leak in split_maildirJeff King, Sep 24, 2015
  4. 03/68 archive-tar: fix minor indentation violationJeff King, Sep 24, 2015
  5. 04/68 fsck: don't fsck alternates for connectivity-only checkJeff King, Sep 24, 2015
  6. 05/68 add xsnprintf helper functionJeff King, Sep 24, 2015
  7. 06/68 add git_path_buf helper functionJeff King, Sep 24, 2015
  8. 07/68 strbuf: make strbuf_complete_line more genericJeff King, Sep 24, 2015
  9. 08/68 add reentrant variants of sha1_to_hex and find_unique_abbrevJeff King, Sep 24, 2015
  10. 09/68 fsck: use strbuf to generate alternate directoriesJeff King, Sep 24, 2015
  11. 10/68 mailsplit: make PATH_MAX buffers dynamicJeff King, Sep 24, 2015
  12. 11/68 trace: use strbuf for quote_crnl outputJeff King, Sep 24, 2015
  13. 12/68 progress: store throughput display in a strbufJeff King, Sep 24, 2015
  14. 13/68 test-dump-cache-tree: avoid overflow of cache-tree nameJeff King, Sep 24, 2015
  15. 14/68 compat/inet_ntop: fix off-by-one in inet_ntop4Jeff King, Sep 24, 2015
  16. 15/68 convert trivial sprintf / strcpy calls to xsnprintfJeff King, Sep 24, 2015
  17. 16/68 archive-tar: use xsnprintf for trivial formattingJeff King, Sep 24, 2015
  18. 17/68 use xsnprintf for generating git object headersJeff King, Sep 24, 2015
  19. 18/68 find_short_object_filename: convert sprintf to xsnprintfJeff King, Sep 24, 2015
  20. 19/68 stop_progress_msg: convert sprintf to xsnprintfJeff King, Sep 24, 2015
  21. 20/68 compat/hstrerror: convert sprintf to snprintfJeff King, Sep 24, 2015
  22. 21/68 grep: use xsnprintf to format failure messageJeff King, Sep 24, 2015
  23. 22/68 entry.c: convert strcpy to xsnprintfJeff King, Sep 24, 2015
  24. 23/68 add_packed_git: convert strcpy into xsnprintfJeff King, Sep 24, 2015
  25. 24/68 http-push: replace strcat with xsnprintfJeff King, Sep 24, 2015
  26. 25/68 receive-pack: convert strncpy to xsnprintfJeff King, Sep 24, 2015
  27. 26/68 replace trivial malloc + sprintf / strcpy calls with xstrfmtJeff King, Sep 24, 2015
  28. 27/68 config: use xstrfmt in normalize_valueJeff King, Sep 24, 2015
  29. 28/68 fetch: replace static buffer with xstrfmtJeff King, Sep 24, 2015
  30. 29/68 use strip_suffix and xstrfmt to replace suffixJeff King, Sep 24, 2015
  31. 30/68 ref-filter: drop sprintf and strcpy callsJeff King, Sep 24, 2015
  32. 31/68 help: drop prepend function in favor of xstrfmtJeff King, Sep 24, 2015
  33. 32/68 mailmap: replace strcpy with xstrdupJeff King, Sep 24, 2015
  34. 33/68 read_branches_file: simplify string handlingJeff King, Sep 24, 2015
  35. 34/68 read_remotes_file: simplify string handlingJeff King, Sep 24, 2015
  36. 35/68 resolve_ref: use strbufs for internal buffersJeff King, Sep 24, 2015
  37. 36/68 upload-archive: convert sprintf to strbufJeff King, Sep 24, 2015
  38. 37/68 remote-ext: simplify git pkt-line generationJeff King, Sep 24, 2015
  39. 38/68 http-push: use strbuf instead of fwrite_bufferJeff King, Sep 24, 2015
  40. 39/68 http-walker: store url in a strbufJeff King, Sep 24, 2015
  41. 40/68 sha1_get_pack_name: use a strbufJeff King, Sep 24, 2015
  42. 41/68 init: use strbufs to store pathsJeff King, Sep 24, 2015
  43. Michael BlumeSep 29, 2015
  44. Jeff KingSep 30, 2015
  45. Junio C HamanoSep 30, 2015
  46. Jeff KingOct 1, 2015
  47. Torsten BögershausenOct 2, 2015
  48. Jeff KingOct 2, 2015
  49. Torsten BögershausenOct 3, 2015
  50. Junio C HamanoOct 3, 2015
  51. Torsten BögershausenOct 3, 2015
  52. Jeff KingOct 4, 2015
  53. Torsten BögershausenOct 4, 2015
  54. Jeff KingOct 5, 2015
  55. 1/3 precompose_utf8: drop unused variableJeff King, Oct 5, 2015
  56. Torsten BögershausenOct 6, 2015
  57. 2/3 probe_utf8_pathname_composition: use internal strbufJeff King, Oct 5, 2015
  58. 3/3 init: use strbufs to store pathsJeff King, Oct 5, 2015
  59. 42/68 apply: convert root string to strbufJeff King, Sep 24, 2015
  60. 43/68 transport: use strbufs for status table "quickref" stringsJeff King, Sep 24, 2015
  61. 44/68 merge-recursive: convert malloc / strcpy to strbufJeff King, Sep 24, 2015
  62. 45/68 enter_repo: convert fixed-size buffers to strbufsJeff King, Sep 24, 2015
  63. 46/68 remove_leading_path: use a strbuf for internal storageJeff King, Sep 24, 2015
  64. 47/68 write_loose_object: convert to strbufJeff King, Sep 24, 2015
  65. 48/68 diagnose_invalid_index_path: use strbuf to avoid strcpy/strcatJeff King, Sep 24, 2015
  66. 49/68 fetch-pack: use argv_array for index-pack / unpack-objectsJeff King, Sep 24, 2015
  67. 50/68 http-push: use an argv_array for setup_revisionsJeff King, Sep 24, 2015
  68. 51/68 stat_tracking_info: convert to argv_arrayJeff King, Sep 24, 2015
  69. 52/68 daemon: use cld->env_array when re-spawningJeff King, Sep 24, 2015
  70. 53/68 use sha1_to_hex_r() instead of strcpyJeff King, Sep 24, 2015
  71. 54/68 drop strcpy in favor of raw sha1_to_hexJeff King, Sep 24, 2015
  72. Eric SunshineSep 24, 2015
  73. Jeff KingSep 25, 2015
  74. 55/68 color: add overflow checks for parsing colorsJeff King, Sep 24, 2015
  75. 56/68 use alloc_ref rather than hand-allocating "struct ref"Jeff King, Sep 24, 2015
  76. 57/68 avoid sprintf and strcpy with flex arraysJeff King, Sep 24, 2015
  77. 58/68 receive-pack: simplify keep_arg computationJeff King, Sep 24, 2015
  78. 59/68 help: clean up kfmclient mungingJeff King, Sep 24, 2015
  79. 60/68 prefer memcpy to strcpyJeff King, Sep 24, 2015
  80. René ScharfeSep 27, 2015
  81. Torsten BögershausenSep 27, 2015
  82. René ScharfeSep 27, 2015
  83. René ScharfeSep 27, 2015
  84. Rasmus VillemoesSep 28, 2015
  85. 61/68 color: add color_set helper for copying raw colorsJeff King, Sep 24, 2015
  86. 62/68 notes: document length of fanout path with a constantJeff King, Sep 24, 2015
  87. 63/68 convert strncpy to memcpyJeff King, Sep 24, 2015
  88. 64/68 fsck: drop inode-sorting codeJeff King, Sep 24, 2015
  89. 65/68 Makefile: drop D_INO_IN_DIRENT build knobJeff King, Sep 24, 2015
  90. 66/68 fsck: use for_each_loose_file_in_objdirJeff King, Sep 24, 2015
  91. Jeff KingSep 26, 2015
  92. 67/68 use strbuf_complete to conditionally append slashJeff King, Sep 24, 2015
  93. 68/68 name-rev: use strip_suffix to avoid magic numbersJeff King, Sep 24, 2015

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.