coccinelle to catch pass-by-value?, was: [PATCH v1 1/2] worktree: do not pass strbuf by value
- From
Jeff King <peff@peff.net>
- Date
- Mar 9, 2026, 19:26 UTC
- Message-ID
- <20260309192600.GC309867@coredump.intra.peff.net>
- In-Reply-To
- <20260308180359.31188-2-deveshigurgaon@gmail.com>
On Sun, Mar 08, 2026 at 06:03:58PM +0000, Deveshi Dwivedi wrote:
> The function only needs the string values, not the strbuf machinery. > Switch it to take const char * and update all callers to pass .buf.
Nice catch. I wonder if we can get the compiler or other static analysis to complain about this mistake. The best I could come up with is:
diff --git a/contrib/coccinelle/strbuf.cocci b/contrib/coccinelle/strbuf.cocci index 5f06105df6..665f56d070 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); + +@@ +expression F, ARG1, ARG2; +struct strbuf SB; +@@ +- F(ARG1, SB, ARG2) ++ F(ARG1, &SB, ARG2) It rewrites a non-pointer argument into a pointer. That's not enough to actually make the code work, but it would alert a developer that they needed to follow-through on the rest of it. Or maybe it would just confuse them without further hints. I think there may be a way to get coccinelle to just emit an error message describing the situation, but it relies on python extensions, which I'm not sure we currently require. Anyway, your patch is obviously good and anything further we do would want to come on top of it. -Peff