git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: "René Scharfe" <l.s.r@web.de>
Cc: Git List <git@vger.kernel.org>, Patrick Steinhardt <ps@pks.im>
Subject: Re: [PATCH] fetch: convert strncmp() with strlen() to starts_with()
Date: Mon, 26 Feb 2024 09:11:55 -0800	[thread overview]
Message-ID: <xmqq1q8zduas.fsf@gitster.g> (raw)
In-Reply-To: <xmqqfrxhjujr.fsf@gitster.g> (Junio C. Hamano's message of "Sat, 24 Feb 2024 15:45:28 -0800")

Junio C Hamano <gitster@pobox.com> writes:

>> -		     !strncmp(rs->items[i].src,
>> -			      ref_namespace[NAMESPACE_TAGS].ref,
>> -			      strlen(ref_namespace[NAMESPACE_TAGS].ref)))) {
>> +		     starts_with(rs->items[i].src,
>> +				 ref_namespace[NAMESPACE_TAGS].ref))) {
>
> The original tries to check that "namespace" fully matches the
> initial part of .src string, which is exactly what starts_with()
> does.  Makes sense.

There are two more such instances in the codebase, easily found with

        $ cat >contrib/coccinelle/starts_with.cocci <<\-EOF
        @@
        expression string, prefix;
        @@
        - !strncmp(string, prefix, strlen(prefix))
        + starts_with(string, prefix)

        @@
        expression string, prefix;
        @@
        - strncmp(string, prefix, strlen(prefix))
        + !starts_with(string, prefix)
        EOF
        $ make contrib/coccinelle/starts_with.cocci.patch

which finds the one you just fixed (naturally, since the Cocci patch
was written by taking inspiration from your fix).

Here is one in the reftable code.  The other one is in xdiff/ I'd
rather not touch (as starts_with() is probably not available there).

diff -u -p a/reftable/refname.c b/reftable/refname.c
--- a/reftable/refname.c
+++ b/reftable/refname.c
@@ -103,7 +103,7 @@ static int modification_has_ref_with_pre
 			}
 		}
 
-		if (strncmp(ref.refname, prefix, strlen(prefix))) {
+		if (!starts_with(ref.refname, prefix)) {
 			err = 1;
 			goto done;
 		}

  reply	other threads:[~2024-02-26 17:12 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-24 21:47 [PATCH] fetch: convert strncmp() with strlen() to starts_with() René Scharfe
2024-02-24 23:45 ` Junio C Hamano
2024-02-26 17:11   ` Junio C Hamano [this message]
2024-02-26 20:04     ` René Scharfe
2024-02-27  7:58       ` Patrick Steinhardt

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

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

  git send-email \
    --in-reply-to=xmqq1q8zduas.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=l.s.r@web.de \
    --cc=ps@pks.im \
    /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.
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 NNTP newsgroup(s).