# [PATCH 1/2] coccinelle: add rules to use strbuf_add() for adding buffers to strbufs

3 messages from 2017-10-01 to 2017-10-02. Participants: René Scharfe, Jeff King.
Thread: https://gitlist.dev/t/46871

## René Scharfe, 2017-10-01 14:49

Subject: [PATCH 1/2] coccinelle: add rules to use strbuf_add() for adding buffers to strbufs
Message-ID: <85605bc6-2e91-7132-ce63-f6e08a08a768@web.de>
URL: https://gitlist.dev/e/85605bc6-2e91-7132-ce63-f6e08a08a768%40web.de

```
strbuf_addf() can be used with "%.*s" to add a buffer of a specific size
to a strbuf, but the resulting code is longer, may require casting the
length to int and is less efficient than simply calling strbuf_add().
Add Coccinelle rules for transforming calls of the former type to the
latter.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
 contrib/coccinelle/strbuf.cocci | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/contrib/coccinelle/strbuf.cocci b/contrib/coccinelle/strbuf.cocci
index 1d580e49b0..700a5f9c13 100644
--- a/contrib/coccinelle/strbuf.cocci
+++ b/contrib/coccinelle/strbuf.cocci
@@ -44,3 +44,15 @@ expression E1, E2;
 @@
 - strbuf_addstr(E1, real_path(E2));
 + strbuf_add_real_path(E1, E2);
+
+@@
+expression sb, buf, len;
+@@
+- strbuf_addf(sb, "%.*s", len, buf)
++ strbuf_add(sb, buf, len)
+
+@@
+expression sb, buf, len;
+@@
+- strbuf_addf(sb, "%.*s", (int)(len), buf)
++ strbuf_add(sb, buf, len)
-- 
2.14.2

```

## René Scharfe, 2017-10-01 14:54

Subject: [PATCH 2/2] commit: use strbuf_add() to add a buffer to a strbuf
Message-ID: <223ef0c8-dac2-747b-41c8-3bad62f4c274@web.de>
URL: https://gitlist.dev/e/223ef0c8-dac2-747b-41c8-3bad62f4c274%40web.de
In-Reply-To: <85605bc6-2e91-7132-ce63-f6e08a08a768@web.de>

```
This is shorter, easier and makes the intent clearer.

Patch generated with Coccinelle and contrib/coccinelle/strbuf.cocci.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
 builtin/commit.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/builtin/commit.c b/builtin/commit.c
index 0f8ddb6866..d186736617 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -542,7 +542,7 @@ static void export_one(const char *var, const char *s, const char *e, int hack)
 	struct strbuf buf = STRBUF_INIT;
 	if (hack)
 		strbuf_addch(&buf, hack);
-	strbuf_addf(&buf, "%.*s", (int)(e - s), s);
+	strbuf_add(&buf, s, e - s);
 	setenv(var, buf.buf, 1);
 	strbuf_release(&buf);
 }
-- 
2.14.2

```

## Jeff King, 2017-10-02 05:18

Subject: Re: [PATCH 2/2] commit: use strbuf_add() to add a buffer to a strbuf
Message-ID: <20171002051855.f3g2sz32ysjd6xu4@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20171002051855.f3g2sz32ysjd6xu4%40sigill.intra.peff.net
In-Reply-To: <223ef0c8-dac2-747b-41c8-3bad62f4c274@web.de>

```
On Sun, Oct 01, 2017 at 04:54:14PM +0200, René Scharfe wrote:

> This is shorter, easier and makes the intent clearer.
> 
> Patch generated with Coccinelle and contrib/coccinelle/strbuf.cocci.

Technically this behaves differently if there is a NUL embedded in the
string. I'd expect that to be unlikely in most cases. And when it is
possible, I'd venture to say that the new behavior is likely to be the
correct one.

This case is fine (and any embedded NUL would be cut off by setenv
anyway).  As a general transformation, this is not as obviously safe as
many of the others.  But since we eyeball the output of coccinelle
anyway, it should be fine (and any code which is _relying_ on that
behavior really ought to be rewritten in a less subtle way).

-Peff

```
