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

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
Previous: Junio C HamanoNext: Deveshi Dwivedi
Message 4 of 8 in “avoid unnecessary strbuf_split*() and strbuf-by-value usage”
  1. 0/2 avoid unnecessary strbuf_split*() and strbuf-by-value usageDeveshi Dwivedi, Mar 8, 2026
  2. 1/2 worktree: do not pass strbuf by valueDeveshi Dwivedi, Mar 8, 2026
  3. Junio C HamanoMar 9, 2026
  4. coccinelle to catch pass-by-value?, was: [PATCH v1 1/2] worktree: do not pass strbuf by valueJeff King, Mar 9, 2026
  5. 2/2 list-objects-filter-options: avoid strbuf_split_str()Deveshi Dwivedi, Mar 8, 2026
  6. Junio C HamanoMar 9, 2026
  7. Jeff KingMar 9, 2026
  8. Jeff KingMar 9, 2026

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.