Skip to content

Commit 65fec23

Browse files
deveshidwivedigitster
authored andcommitted
coccinelle: detect struct strbuf passed by value
Passing a struct strbuf by value to a function copies the struct but shares the underlying character array between caller and callee. If the callee causes a reallocation, the caller's copy becomes a dangling pointer, leading to a double-free when strbuf_release() is called. There is no coccinelle rule to catch this pattern. Jeff King suggested adding one during review of the write_worktree_linking_files() fix [1], and noted that a reporting rule using coccinelle's Python scripting extensions could emit a descriptive warning, but we do not currently require Python support in coccinelle. Add a transformation rule that rewrites a by-value strbuf parameter to a pointer. The detection is identical to what a Python-based reporting rule would catch; only the presentation differs. The resulting diff will not produce compilable code on its own (callers and the function body still need updating), but the spatch output alerts the developer that the signature needs attention. This is consistent with the other rules in strbuf.cocci, which also rewrite to the preferred form. [1] https://lore.kernel.org/git/20260309192600.GC309867@coredump.intra.peff.net/ Signed-off-by: Deveshi Dwivedi <deveshigurgaon@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 6e84af9 commit 65fec23

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

contrib/coccinelle/strbuf.cocci

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,14 @@ expression E1, E2;
6060
@@
6161
- strbuf_addstr(E1, real_path(E2));
6262
+ strbuf_add_real_path(E1, E2);
63+
64+
@@
65+
identifier fn, param;
66+
@@
67+
fn(...,
68+
- struct strbuf param
69+
+ struct strbuf *param
70+
,...)
71+
{
72+
...
73+
}

0 commit comments

Comments
 (0)