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

[PATCH] strbuf.cocci: suggest strbuf_addbuf() to add one strbuf to an other

From
SZEDER Gábor <szeder.dev@gmail.com>
Date
Jan 25, 2019, 12:25 UTC
Message-ID
<20190125122517.29026-1-szeder.dev@gmail.com>
The best way to add one strbuf to an other is via:
  strbuf_addbuf(&sb, &sb2);
This is a bit more idiomatic and efficient than:
  strbuf_addstr(&sb, sb2.buf);

because the size of the second strbuf is known and thus it can spare a strlen() call, and much more so than:

  strbuf_addf(&sb, "%s", sb2.buf);
because it can spare the whole vsnprintf() formatting magic.

Add new semantic patches to 'contrib/coccinelle/strbuf.cocci' to catch these undesired patterns and to suggest strbuf_addbuf() instead.

Luckily, our codebase is already clean from any such undesired patterns (but one of the in-flight topics just tried to sneak in such a strbuf_addf() call).

Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
---
Inspired by:
  https://public-inbox.org/git/20190125112203.GB6702@szeder.dev/
 contrib/coccinelle/strbuf.cocci | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
diff --git a/contrib/coccinelle/strbuf.cocci b/contrib/coccinelle/strbuf.cocci
index e34eada1ad..d9ada69b43 100644
--- a/contrib/coccinelle/strbuf.cocci
+++ b/contrib/coccinelle/strbuf.cocci
@@ -12,6 +12,36 @@ constant fmt !~ "%";
 )
   );
 
+@@
+expression E;
+struct strbuf SB;
+format F =~ "s";
+@@
+- strbuf_addf(E, "%@F@", SB.buf);
++ strbuf_addbuf(E, &SB);
+
+@@
+expression E;
+struct strbuf *SBP;
+format F =~ "s";
+@@
+- strbuf_addf(E, "%@F@", SBP->buf);
++ strbuf_addbuf(E, SBP);
+
+@@
+expression E;
+struct strbuf SB;
+@@
+- strbuf_addstr(E, SB.buf);
++ strbuf_addbuf(E, &SB);
+
+@@
+expression E;
+struct strbuf *SBP;
+@@
+- strbuf_addstr(E, SBP->buf);
++ strbuf_addbuf(E, SBP);
+
 @@
 expression E1, E2;
 format F =~ "s";
-- 
2.20.1.642.gc55a771460
Message 1 of 1 in “strbuf.cocci: suggest strbuf_addbuf() to add one strbuf to an other”
  1. strbuf.cocci: suggest strbuf_addbuf() to add one strbuf to an otherSZEDER Gábor, Jan 25, 2019

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.