Volume XXII, number 280Wednesday, October 7, 2026Latest message 2 hours ago

The Git List

News and archive of git@vger.kernel.org, since April 2005

patchcocci: strbuf.buf is never NULL

2 messages between Mar 19, 2026 and Mar 20, 2026, from Junio C Hamano, Derrick Stolee.

Plain Markdown or JSON for tools and agents. Diffs are folded; open one to read it.

Junio C HamanoMar 19, 2026, 22:39 UTC on lore

We recently noticed one old code from 19 years ago protecting against an ancient strbuf convention that the .buf member can be NULL for an empty strbuf. As that is no longer the case in the modern codebase, let's catch such a construct.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 * Comes directly on top of my recent rerere.c clean-up patch.
 contrib/coccinelle/strbuf.cocci | 7 +++++++
 1 file changed, 7 insertions(+)
Show changes to contrib/coccinelle/strbuf.cocci +7 −0
diff --git a/contrib/coccinelle/strbuf.cocci b/contrib/coccinelle/strbuf.cocci
index 5f06105df6..13f0ad2679 100644
--- a/contrib/coccinelle/strbuf.cocci
+++ b/contrib/coccinelle/strbuf.cocci
@@ -60,3 +60,10 @@ expression E1, E2;
 @@
 - strbuf_addstr(E1, real_path(E2));
 + strbuf_add_real_path(E1, E2);
+
+// In modern codebase, .buf member of an empty strbuf is not NULL.
+@@
+struct strbuf SB;
+@@
+- SB.buf ? SB.buf : ""
++ SB.buf
-- 
2.53.0-816-g44373249a2
Derrick StoleeMar 20, 2026, 16:31 UTC in reply to Junio C Hamano on lore

Re: [PATCH] cocci: strbuf.buf is never NULL

On 3/19/2026 6:39 PM, Junio C Hamano wrote:
> We recently noticed one old code from 19 years ago protecting
> against an ancient strbuf convention that the .buf member can be
> NULL for an empty strbuf.  As that is no longer the case in the
> modern codebase, let's catch such a construct.
Show 6 quoted lines
> +// In modern codebase, .buf member of an empty strbuf is not NULL.
> +@@
> +struct strbuf SB;
> +@@
> +- SB.buf ? SB.buf : ""
> ++ SB.buf
LGTM. I like to see these structural patterns encoded as rules.

Thanks, -Stolee

Back to recent threads

[PATCH] cocci: strbuf.buf is never NULL | The Git List