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

Re: [PATCH] Rework strbuf API and semantics.

From
Pierre Habouzit <madcoder@debian.org>
Date
Sep 6, 2007, 09:49 UTC
Message-ID
<20070906094926.GC8451@artemis.corp>
In-Reply-To
<7vlkbkb0na.fsf@gitster.siamese.dyndns.org>
On Thu, Sep 06, 2007 at 09:31:37AM +0000, Junio C Hamano wrote:
Show 43 quoted lines
> Pierre Habouzit <madcoder@debian.org> writes:
> 
> >   A strbuf can be used to store byte arrays, or as an extended string
> > library. The `buf' member can be passed to any C legacy string function,
> > because strbuf operations always ensure there is a terminating \0 at the end
> > of the buffer, not accounted in the `len' field of the structure.
> >
> >   A strbuf can be used to generate a string/buffer whose final size is not
> > really known, and then "strbuf_detach" can be used to get the built buffer,
> > and keep the wrapping "strbuf" structure usable for further work again.
> >
> >   Other interesting feature: strbuf_grow(sb, size) ensure that there is
> > enough allocated space in `sb' to put `size' new octets of data in the
> > buffer. It helps avoiding reallocating data for nothing when the problem the
> > strbuf helps to solve has a known typical size.
> 
> "Rework API semantics" needs to be accompanied with an API
> description, perhaps at the beginning of each externally
> visible function.
> 
> Also the commit log message needs to explain what the old
> semantics was and what the improved one is, to highlight the
> changes needed to the callers.  Especially...
> 
> > @@ -1657,11 +1656,11 @@ static void *cmd_data (size_t *size)
> >  			if (term_len == command_buf.len
> >  				&& !strcmp(term, command_buf.buf))
> >  				break;
> > -			ALLOC_GROW(buffer, length + command_buf.len, sz);
> > +			ALLOC_GROW(buffer, length + command_buf.len + 1, sz);
> >  			memcpy(buffer + length,
> >  				command_buf.buf,
> > -				command_buf.len - 1);
> > -			length += command_buf.len - 1;
> > +				command_buf.len);
> > +			length += command_buf.len;
> >  			buffer[length++] = '\n';
> >  		}
> >  		free(term);
> 
> .... it is not all obvious why these off-by-one changes are
> needed without such a description.  The other hunks in this
> patch to this file are all such changes.
  Yes, as I suppose you know, but I state it here again so that
everybody understands, before strbuf's were merely a byte array, not
necessarily NUL-terminated. Hence many parts of the code that wanted to
pass the buffer to str* functions had to manually insert a NUL, hence it
was accounted in the length of the buffer.
  Now, we always have a NUL after the "official" end of the buffer, so
it's not needed anymore. The off-by-ones are just that. The hunk you
quote is one where git's code was messing with strbufs internals
directly, so the ALLOC_GROW has to take the 1 octed needed to maintain
the internal invariant. Though, the patch after this one rewrites the
hunk to use strbuf's API's.
Show 10 quoted lines
> 
> > -static void inline strbuf_add(struct strbuf *sb, int ch) {
> 
> > +static inline void strbuf_addch(struct strbuf *sb, size_t c) {
> > +	strbuf_grow(sb, 1);
> > +	sb->buf[sb->len++] = c;
> > +	sb->buf[sb->len] = '\0';
> > +}
> 
> You certainly did not mean size_t wide characters.
  Oh boy, now I've been red-handed of :%s/\<int\>/\<size_t\>/ :)
  I'll repost a _clean_ patch series soon with those things fixed, and
the wrong overflow test (that should be a >= and not a >) as well, as we
discussed it on IRC before.
  Oh and FWIW I believe the details of the NUL always after the buffer
array has to be in strbuf.h and not in the commit comment, maybe I'll
put it in both to make everybody happy.
-- 
·O·  Pierre Habouzit
··O                                                madcoder@debian.org
OOO                                                http://www.madism.org
Previous: Junio C HamanoNext: Junio C Hamano
Message 33 of 90 in “strbuf API”
  1. Pierre HabouzitSep 2, 2007
  2. Johan HerlandSep 3, 2007
  3. Pierre HabouzitSep 3, 2007
  4. Miles BaderSep 4, 2007
  5. strbuf new semantics, let's give it a tryPierre Habouzit, Sep 4, 2007
  6. Rework strbuf API and semantics.Pierre Habouzit, Sep 4, 2007
  7. Johannes SchindelinSep 4, 2007
  8. Pierre HabouzitSep 4, 2007
  9. Andreas EricssonSep 4, 2007
  10. Rework strbuf API and semantics.Pierre Habouzit, Sep 4, 2007
  11. Johannes SchindelinSep 4, 2007
  12. Pierre HabouzitSep 4, 2007
  13. Wincent ColaiutaSep 4, 2007
  14. Simplify strbuf uses in fast-import.c using the proper functions.Pierre Habouzit, Sep 4, 2007
  15. René ScharfeSep 4, 2007
  16. René ScharfeSep 4, 2007
  17. Pierre HabouzitSep 5, 2007
  18. Junio C HamanoSep 5, 2007
  19. Pierre HabouzitSep 5, 2007
  20. Rework strbuf API and semantics.Pierre Habouzit, Sep 5, 2007
  21. Simplify strbuf uses in archive-tar.c using the proper functions.Pierre Habouzit, Sep 5, 2007
  22. Simplify strbuf uses in fast-import.c using the proper functions.Pierre Habouzit, Sep 5, 2007
  23. Use proper strbuf API, and also simplify cmd_data code.Pierre Habouzit, Sep 5, 2007
  24. Simplify write_tree using strbuf's.Pierre Habouzit, Sep 5, 2007
  25. Further strbuf re-engineering.Pierre Habouzit, Sep 5, 2007
  26. Eradicate yet-another-buffer implementation in buitin-rerere.cPierre Habouzit, Sep 5, 2007
  27. More strbuf uses in cache-tree.c.Pierre Habouzit, Sep 5, 2007
  28. Junio C HamanoSep 19, 2007
  29. Pierre HabouzitSep 5, 2007
  30. Junio C HamanoSep 19, 2007
  31. Pierre HabouzitSep 19, 2007
  32. Junio C HamanoSep 6, 2007
  33. Pierre HabouzitSep 6, 2007
  34. Junio C HamanoSep 6, 2007
  35. Pierre HabouzitSep 6, 2007
  36. Use proper strbuf API, and also simplify cmd_data code.Pierre Habouzit, Sep 4, 2007
  37. Miles BaderSep 5, 2007
  38. Add strbuf_fread, use it in fast-import.c.Pierre Habouzit, Sep 4, 2007
  39. Matthieu MoySep 3, 2007
  40. Pierre HabouzitSep 3, 2007
  41. Matthieu MoySep 3, 2007
  42. Junio C HamanoSep 3, 2007
  43. Pierre HabouzitSep 3, 2007
  44. Johannes SchindelinSep 3, 2007
  45. strbuf new API, take 2 for inclusionPierre Habouzit, Sep 6, 2007
  46. 1/7 Rework strbuf API and semantics.Pierre Habouzit, Sep 6, 2007
  47. 2/7 Simplify strbuf uses in archive-tar.c using the proper functions.Pierre Habouzit, Sep 6, 2007
  48. 3/7 Use proper strbuf API, and also simplify cmd_data code.Pierre Habouzit, Sep 6, 2007
  49. 4/7 Simplify write_tree using strbuf's.Pierre Habouzit, Sep 6, 2007
  50. 5/7 Further strbuf re-engineering.Pierre Habouzit, Sep 6, 2007
  51. 6/7 Eradicate yet-another-buffer implementation in buitin-rerere.cPierre Habouzit, Sep 6, 2007
  52. 7/7 More strbuf uses in cache-tree.c.Pierre Habouzit, Sep 6, 2007
  53. Johannes SchindelinSep 6, 2007
  54. Pierre HabouzitSep 6, 2007
  55. David KastrupSep 6, 2007
  56. Pierre HabouzitSep 6, 2007
  57. Junio C HamanoSep 7, 2007
  58. Pierre HabouzitSep 7, 2007
  59. Kristian HøgsbergSep 6, 2007
  60. Pierre HabouzitSep 6, 2007
  61. Kristian HøgsbergSep 6, 2007
  62. Pierre HabouzitSep 6, 2007
  63. René ScharfeSep 6, 2007
  64. Johannes SchindelinSep 6, 2007
  65. Jeff KingSep 6, 2007
  66. David KastrupSep 6, 2007
  67. Jeff KingSep 6, 2007
  68. David KastrupSep 6, 2007
  69. Jeff KingSep 6, 2007
  70. David KastrupSep 6, 2007
  71. Johannes SixtSep 6, 2007
  72. David KastrupSep 6, 2007
  73. Jeff KingSep 6, 2007
  74. Kristian HøgsbergSep 6, 2007
  75. Jeff KingSep 6, 2007
  76. Pierre HabouzitSep 6, 2007
  77. Jeff KingSep 6, 2007
  78. Pierre HabouzitSep 6, 2007
  79. Use strbufs in commit.c (pretty printing)Pierre Habouzit, Sep 8, 2007
  80. 1/3 Add strbuf_rtrim (to remove trailing spaces).Pierre Habouzit, Sep 8, 2007
  81. 2/3 Change semantics of interpolate to work like snprintf.Pierre Habouzit, Sep 8, 2007
  82. 3/3 Rework pretty_print_commit to use strbufs instead of custom buffers.Pierre Habouzit, Sep 8, 2007
  83. David KastrupSep 8, 2007
  84. Pierre HabouzitSep 8, 2007
  85. Pierre HabouzitSep 8, 2007
  86. René ScharfeSep 8, 2007
  87. Pierre HabouzitSep 8, 2007
  88. René ScharfeSep 8, 2007
  89. Pierre HabouzitSep 8, 2007
  90. Pierre HabouzitSep 8, 2007

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.