Skip to content

Commit e022215

Browse files
jrngitster
authored andcommitted
strbuf doc: reuse after strbuf_release is fine
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 <[email protected]> Signed-off-by: Jonathan Nieder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4010f1d commit e022215

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

strbuf.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,22 @@ extern char strbuf_slopbuf[];
8282
extern void strbuf_init(struct strbuf *, size_t);
8383

8484
/**
85-
* Release a string buffer and the memory it used. You should not use the
86-
* string buffer after using this function, unless you initialize it again.
85+
* Release a string buffer and the memory it used. After this call, the
86+
* strbuf points to an empty string that does not need to be free()ed, as
87+
* if it had been set to `STRBUF_INIT` and never modified.
88+
*
89+
* To clear a strbuf in preparation for further use without the overhead
90+
* of free()ing and malloc()ing again, use strbuf_reset() instead.
8791
*/
8892
extern void strbuf_release(struct strbuf *);
8993

9094
/**
9195
* Detach the string from the strbuf and returns it; you now own the
9296
* storage the string occupies and it is your responsibility from then on
9397
* to release it with `free(3)` when you are done with it.
98+
*
99+
* The strbuf that previously held the string is reset to `STRBUF_INIT` so
100+
* it can be reused after calling this function.
94101
*/
95102
extern char *strbuf_detach(struct strbuf *, size_t *);
96103

0 commit comments

Comments
 (0)