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

[PATCH v2] strbuf doc: reuse after strbuf_release is fine

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Oct 4, 2017, 02:39 UTC
Message-ID
<20171004023954.GK19555@aiede.mtv.corp.google.com>
In-Reply-To
<20171003234919.qsr54ncmw6cihowi@sigill.intra.peff.net>

strbuf_release leaves the strbuf in a valid, initialized state, so there is no need to call strbuf_init after it.

Moreover, this is not likely to change in the future: strbuf_release leaving the strbuf in a valid state has been easy to maintain and has been very helpful for Git's robustness and simplicity (e.g., preventing use-after-free vulnerabilities).

Document the semantics so the next generation of Git developers can become familiar with them without reading the implementation. It is still not advisable to call strbuf_release too often because it is wasteful, so add a note pointing to strbuf_reset for that.

The same semantics apply to strbuf_detach. Add a similar note to its docstring to make that clear.

Improved-by: Jeff King <peff@peff.net>
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
Jeff King wrote:
Show 6 quoted lines
> I think it's actually OK to use the string buffer after this function.
> It's just an empty string.
>
> Perhaps we should be more explicit: this releases any resources and
> resets to a pristine, empty state. I suspect strbuf_detach() probably
> should make the same claim.
Like this?

Thanks, Jonathan

 strbuf.h | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
diff --git a/strbuf.h b/strbuf.h
index 7496cb8ec5..249df86711 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -82,8 +82,12 @@ extern char strbuf_slopbuf[];
 extern void strbuf_init(struct strbuf *, size_t);
 
 /**
- * Release a string buffer and the memory it used. You should not use the
- * string buffer after using this function, unless you initialize it again.
+ * Release a string buffer and the memory it used. After this call, the
+ * strbuf points to an empty string that does not need to be free()ed, as
+ * if it had been set to `STRBUF_INIT` and never modified.
+ *
+ * To clear a strbuf in preparation for further use without the overhead
+ * of free()ing and malloc()ing again, use strbuf_reset() instead.
  */
 extern void strbuf_release(struct strbuf *);
 
@@ -91,6 +95,9 @@ extern void strbuf_release(struct strbuf *);
  * Detach the string from the strbuf and returns it; you now own the
  * storage the string occupies and it is your responsibility from then on
  * to release it with `free(3)` when you are done with it.
+ *
+ * The strbuf that previously held the string is reset to `STRBUF_INIT` so
+ * it can be reused after calling this function.
  */
 extern char *strbuf_detach(struct strbuf *, size_t *);
 
-- 
2.14.2.920.gcf0c67979c
Previous: Junio C HamanoNext: Junio C Hamano
Message 7 of 9 in “branch: reset instead of release a strbuf”
  1. branch: reset instead of release a strbufStefan Beller, Oct 3, 2017
  2. Jonathan NiederOct 3, 2017
  3. branch: reset instead of release a strbufStefan Beller, Oct 3, 2017
  4. Jonathan NiederOct 3, 2017
  5. Jeff KingOct 3, 2017
  6. Junio C HamanoOct 4, 2017
  7. strbuf doc: reuse after strbuf_release is fineJonathan Nieder, Oct 4, 2017
  8. Junio C HamanoOct 4, 2017
  9. Jeff KingOct 4, 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.