{"thread":{"id":"46871","subject":"[PATCH 1/2] coccinelle: add rules to use strbuf_add() for adding buffers to strbufs","startedAt":"2017-10-01T14:49:44Z","lastAt":"2017-10-02T05:19:02Z","messageCount":3,"participants":["René Scharfe","Jeff King"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"329286","messageId":"85605bc6-2e91-7132-ce63-f6e08a08a768@web.de","threadId":"46871","inReplyTo":null,"subject":"[PATCH 1/2] coccinelle: add rules to use strbuf_add() for adding buffers to strbufs","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2017-10-01T14:49:35Z","receivedAt":"2017-10-01T14:49:44Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"strbuf_addf() can be used with \"%.*s\" to add a buffer of a specific size\nto a strbuf, but the resulting code is longer, may require casting the\nlength to int and is less efficient than simply calling strbuf_add().\nAdd Coccinelle rules for transforming calls of the former type to the\nlatter.\n\nSigned-off-by: Rene Scharfe <l.s.r@web.de>\n---\n contrib/coccinelle/strbuf.cocci | 12 ++++++++++++\n 1 file changed, 12 insertions(+)\n\ndiff --git a/contrib/coccinelle/strbuf.cocci b/contrib/coccinelle/strbuf.cocci\nindex 1d580e49b0..700a5f9c13 100644\n--- a/contrib/coccinelle/strbuf.cocci\n+++ b/contrib/coccinelle/strbuf.cocci\n@@ -44,3 +44,15 @@ expression E1, E2;\n @@\n - strbuf_addstr(E1, real_path(E2));\n + strbuf_add_real_path(E1, E2);\n+\n+@@\n+expression sb, buf, len;\n+@@\n+- strbuf_addf(sb, \"%.*s\", len, buf)\n++ strbuf_add(sb, buf, len)\n+\n+@@\n+expression sb, buf, len;\n+@@\n+- strbuf_addf(sb, \"%.*s\", (int)(len), buf)\n++ strbuf_add(sb, buf, len)\n-- \n2.14.2\n"},{"id":"329287","messageId":"223ef0c8-dac2-747b-41c8-3bad62f4c274@web.de","threadId":"46871","inReplyTo":"85605bc6-2e91-7132-ce63-f6e08a08a768@web.de","subject":"[PATCH 2/2] commit: use strbuf_add() to add a buffer to a strbuf","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2017-10-01T14:54:14Z","receivedAt":"2017-10-01T14:54:23Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"This is shorter, easier and makes the intent clearer.\n\nPatch generated with Coccinelle and contrib/coccinelle/strbuf.cocci.\n\nSigned-off-by: Rene Scharfe <l.s.r@web.de>\n---\n builtin/commit.c | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/builtin/commit.c b/builtin/commit.c\nindex 0f8ddb6866..d186736617 100644\n--- a/builtin/commit.c\n+++ b/builtin/commit.c\n@@ -542,7 +542,7 @@ static void export_one(const char *var, const char *s, const char *e, int hack)\n \tstruct strbuf buf = STRBUF_INIT;\n \tif (hack)\n \t\tstrbuf_addch(&buf, hack);\n-\tstrbuf_addf(&buf, \"%.*s\", (int)(e - s), s);\n+\tstrbuf_add(&buf, s, e - s);\n \tsetenv(var, buf.buf, 1);\n \tstrbuf_release(&buf);\n }\n-- \n2.14.2\n"},{"id":"329391","messageId":"20171002051855.f3g2sz32ysjd6xu4@sigill.intra.peff.net","threadId":"46871","inReplyTo":"223ef0c8-dac2-747b-41c8-3bad62f4c274@web.de","subject":"Re: [PATCH 2/2] commit: use strbuf_add() to add a buffer to a strbuf","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2017-10-02T05:18:56Z","receivedAt":"2017-10-02T05:19:02Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Sun, Oct 01, 2017 at 04:54:14PM +0200, René Scharfe wrote:\n\n> This is shorter, easier and makes the intent clearer.\n> \n> Patch generated with Coccinelle and contrib/coccinelle/strbuf.cocci.\n\nTechnically this behaves differently if there is a NUL embedded in the\nstring. I'd expect that to be unlikely in most cases. And when it is\npossible, I'd venture to say that the new behavior is likely to be the\ncorrect one.\n\nThis case is fine (and any embedded NUL would be cut off by setenv\nanyway).  As a general transformation, this is not as obviously safe as\nmany of the others.  But since we eyeball the output of coccinelle\nanyway, it should be fine (and any code which is _relying_ on that\nbehavior really ought to be rewritten in a less subtle way).\n\n-Peff\n"}]}