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

Re: [PATCH] branch: reset instead of release a strbuf

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Oct 3, 2017, 22:24 UTC
Message-ID
<20171003222414.GC19555@aiede.mtv.corp.google.com>
In-Reply-To
<20171003221740.26325-1-sbeller@google.com>
Hi,
Stefan Beller wrote:
> Our documentation advises to not re-use a strbuf, after strbuf_release
> has been called on it. Use the proper reset instead.
>
> Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
This is indeed
Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
Thank you.
> Signed-off-by: Stefan Beller <sbeller@google.com>
> ---
>  builtin/branch.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
Here's a patch to address the surprising strbuf.h advice.
-- >8 --
Subject: strbuf: do not encourage init-after-release

strbuf_release already leaves the strbuf in a valid, initialized state, so there is not 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).

It is still not advisable to call strbuf_release until done using a strbuf because it is wasteful, so keep that part of the advice.

Reported-by: Stefan Beller <sbeller@google.com>
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
 strbuf.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/strbuf.h b/strbuf.h
index 7496cb8ec5..6e175c3694 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -83,7 +83,7 @@ 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.
+ * string buffer after using this function.
  */
 extern void strbuf_release(struct strbuf *);
 
-- 
2.14.2.920.gcf0c67979c
Previous: Stefan BellerNext: Jeff King
Message 4 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.