Skip to content

Commit 353d84c

Browse files
rscharfegitster
authored andcommitted
coccicheck: make transformation for strbuf_addf(sb, "...") more precise
We can replace strbuf_addf() calls that just add a simple string with calls to strbuf_addstr() to make the intent clearer. We need to be careful if that string contains printf format specifications like %%, though, as a simple replacement would change the output. Add checks to the semantic patch to make sure we only perform the transformation if the second argument is a string constant (possibly translated) that doesn't contain any percent signs. Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f937d78 commit 353d84c

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

contrib/coccinelle/strbuf.cocci

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,31 @@
1+
@ strbuf_addf_with_format_only @
2+
expression E;
3+
constant fmt;
14
@@
2-
expression E1, E2;
5+
strbuf_addf(E,
6+
(
7+
fmt
8+
|
9+
_(fmt)
10+
)
11+
);
12+
13+
@ script:python @
14+
fmt << strbuf_addf_with_format_only.fmt;
315
@@
4-
- strbuf_addf(E1, E2);
5-
+ strbuf_addstr(E1, E2);
16+
cocci.include_match("%" not in fmt)
17+
18+
@ extends strbuf_addf_with_format_only @
19+
@@
20+
- strbuf_addf
21+
+ strbuf_addstr
22+
(E,
23+
(
24+
fmt
25+
|
26+
_(fmt)
27+
)
28+
);
629

730
@@
831
expression E1, E2;

0 commit comments

Comments
 (0)