Skip to contentSkip to conversation

cocci: strbuf.buf is never NULL

PATCH In review Junio C Hamano 2 participants 2 messages Mar 19 – Mar 20

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cocci: strbuf.buf is never NULL

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.

---
* Comes directly on top of my recent rerere.c clean-up patch.

 contrib/coccinelle/strbuf.cocci | 7 +++++++
 1 file changed, 7 insertions(+)
contrib/coccinelle/strbuf.cocci +7 -0
index 5f06105df6..13f0ad2679 100644
@@ -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
Signature · 1 line
2.53.0-816-g44373249a2
#1 lore raw

contrib/coccinelle/strbuf.cocci expression E1, E2; · line 69

Quoted 11 lines · Junio C Hamano

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.

contrib/coccinelle/strbuf.cocci +6 -0
+// 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