git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Andrei Rybak <rybak.a.v@gmail.com>
To: git@vger.kernel.org
Cc: "SZEDER Gábor" <szeder.dev@gmail.com>,
	"Junio C Hamano" <gitster@pobox.com>, "Jeff King" <peff@peff.net>,
	"Duy Nguyen" <pclouds@gmail.com>
Subject: [PATCH v2 1/2] mailinfo: use starts_with() for clarity
Date: Mon,  1 Apr 2019 23:53:33 +0200	[thread overview]
Message-ID: <20190401215334.18678-1-rybak.a.v@gmail.com> (raw)
In-Reply-To: <20190331220104.31628-1-rybak.a.v@gmail.com>

Existing checks using memcmp(3) never read past the end of the line,
because all substrings we are interested in are two characters long, and
the outer loop guarantees we have at least one character. So at most we
will look at the NUL.

However, this is too subtle and may lead to bugs in code which copies
this behavior without realizing substring length requirement.  So use
starts_with() instead, which will stop at NUL regardless of the length
of the prefix. Remove extra pair of parentheses while we are here.

Helped-by: Jeff King <peff@peff.net>
Signed-off-by: Andrei Rybak <rybak.a.v@gmail.com>
---

On Mon, Apr 01, 2019 at 06:11:57 -0400, Jeff King wrote:
> I wonder if it's worth re-writing it like:

Turned Peff's suggestion into a patch.

 mailinfo.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/mailinfo.c b/mailinfo.c
index b395adbdf2..f4aaa89788 100644
--- a/mailinfo.c
+++ b/mailinfo.c
@@ -693,8 +693,8 @@ static int is_scissors_line(const char *line)
 			perforation++;
 			continue;
 		}
-		if ((!memcmp(c, ">8", 2) || !memcmp(c, "8<", 2) ||
-		     !memcmp(c, ">%", 2) || !memcmp(c, "%<", 2))) {
+		if (starts_with(c, ">8") || starts_with(c, "8<") ||
+		    starts_with(c, ">%") || starts_with(c, "%<")) {
 			in_perforation = 1;
 			perforation += 2;
 			scissors += 2;
-- 
2.21.0


  parent reply	other threads:[~2019-04-01 21:53 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-31 22:01 [PATCH] mailinfo: support Unicode scissors Andrei Rybak
2019-03-31 23:09 ` SZEDER Gábor
2019-04-01  9:07   ` Junio C Hamano
2019-04-01 10:11   ` Jeff King
2019-04-01  9:27 ` Duy Nguyen
2019-04-01 21:54   ` Andrei Rybak
2019-04-01 21:53 ` Andrei Rybak [this message]
2019-04-01 21:53   ` [PATCH v2 2/2] " Andrei Rybak
2019-04-02 14:36     ` Jeff King
2019-04-03  6:47       ` Junio C Hamano
2019-04-02 14:28   ` [PATCH v2 1/2] mailinfo: use starts_with() for clarity Jeff King
2021-06-08 20:48   ` [PATCH] mailinfo: use starts_with() when checking scissors Andrei Rybak
2021-06-08 21:57     ` Jeff King
2021-06-09  2:22       ` Junio C Hamano
2021-06-09  3:59         ` Jeff King
2021-06-09  5:11           ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190401215334.18678-1-rybak.a.v@gmail.com \
    --to=rybak.a.v@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=pclouds@gmail.com \
    --cc=peff@peff.net \
    --cc=szeder.dev@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).