git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "René Scharfe" <l.s.r@web.de>
To: Jeff King <peff@peff.net>
Cc: Duy Nguyen <pclouds@gmail.com>,
	Git Mailing List <git@vger.kernel.org>,
	Junio C Hamano <gitster@pobox.com>
Subject: Re: [PATCH v2 2/2] sha1_file: use strncmp for string comparison
Date: Mon, 30 Jun 2014 18:35:50 +0200	[thread overview]
Message-ID: <53B191E6.7010509@web.de> (raw)
In-Reply-To: <20140630134317.GB14799@sigill.intra.peff.net>

Am 30.06.2014 15:43, schrieb Jeff King:
> On Sun, Jun 29, 2014 at 07:56:25AM +0200, René Scharfe wrote:
> 
>> Avoid overrunning the existing pack name (p->pack_name, a C string) in
>> the case that the new path is longer by using strncmp instead of memcmp
>> for comparing.  While at it, replace the magic constant 4 with a
>> strlen call to document its meaning.
> 
> An unwritten assumption with this code is that we have "foo.idx" and we
> want to make sure that we are matching "foo.pack" from the existing pack
> list. Both before and after your patch, I think we would match
> "foobar.pack".

Yes, indeed.

> It's probably not a big deal, as we don't expect random
> junk in the pack directory, but I wonder if it would be better to be
> explicit, like:

<snip>

Here's a simpler approach:

-- >8 --
Subject: [PATCH v2 3/2] sha1_file: more precise packname matching

Consider the full length of the already loaded pack names when checking
for duplicates.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
 sha1_file.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/sha1_file.c b/sha1_file.c
index b7ad6c1..a13fbbd 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -1206,10 +1206,12 @@ static void prepare_packed_git_one(char *objdir, int local)
 		strbuf_addstr(&path, de->d_name);
 
 		if (has_extension(de->d_name, ".idx")) {
+			size_t len = path.len - strlen(".idx");
+
 			/* Don't reopen a pack we already have. */
 			for (p = packed_git; p; p = p->next) {
-				if (!strncmp(path.buf, p->pack_name,
-					     path.len - strlen(".idx")))
+				if (!strncmp(p->pack_name, path.buf, len) &&
+				    !strcmp(p->pack_name + len, ".pack"))
 					break;
 			}
 			if (p == NULL &&
-- 
2.0.0

  parent reply	other threads:[~2014-06-30 16:36 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-28 14:47 [PATCH 1/2] sha1_file: replace PATH_MAX buffer wirh strbuf in prepare_packed_git_one() René Scharfe
2014-06-28 15:01 ` [PATCH 2/4] sha1_file: use strncmp for string comparison René Scharfe
2014-06-29  1:21 ` [PATCH 1/2] sha1_file: replace PATH_MAX buffer wirh strbuf in prepare_packed_git_one() Duy Nguyen
2014-06-29  5:43   ` [PATCH v2 1/2] sha1_file: replace PATH_MAX buffer wirh strbuf in, prepare_packed_git_one() René Scharfe
2014-06-29  5:56     ` [PATCH v2 2/2] sha1_file: use strncmp for string comparison René Scharfe
2014-06-30 13:43       ` Jeff King
2014-06-30 13:59         ` Duy Nguyen
2014-06-30 14:22           ` Jeff King
2014-06-30 16:43             ` René Scharfe
2014-06-30 16:45               ` Jeff King
2014-06-30 16:35         ` René Scharfe [this message]
2014-06-30 16:46           ` Jeff King
2014-06-30 16:55         ` [PATCH 0/9] add strip_suffix as an alternative to ends_with Jeff King
2014-06-30 16:55           ` [PATCH 1/9] sha1_file: replace PATH_MAX buffer with strbuf in prepare_packed_git_one() Jeff King
2014-06-30 16:57           ` [PATCH 2/9] add strip_suffix function Jeff King
2014-07-02 15:54             ` Junio C Hamano
2014-07-02 16:38               ` Jeff King
2014-07-02 17:41                 ` Junio C Hamano
2014-06-30 16:58           ` [PATCH 3/9] implement ends_with via strip_suffix Jeff King
2014-06-30 16:58           ` [PATCH 4/9] replace has_extension with ends_with Jeff King
2014-06-30 16:58           ` [PATCH 5/9] use strip_suffix instead of ends_with in simple cases Jeff King
2014-06-30 16:59           ` [PATCH 6/9] index-pack: use strip_suffix to avoid magic numbers Jeff King
2014-06-30 17:01           ` [PATCH 7/9] strbuf: implement strbuf_strip_suffix Jeff King
2014-06-30 17:02           ` [PATCH 8/9] verify-pack: use strbuf_strip_suffix Jeff King
2014-06-30 17:04           ` [PATCH 9/9] prepare_packed_git_one: refactor duplicate-pack check Jeff King
2014-07-01  1:00           ` [PATCH 0/9] add strip_suffix as an alternative to ends_with Junio C Hamano
2014-06-30 13:24     ` [PATCH v2 1/2] sha1_file: replace PATH_MAX buffer wirh strbuf in, prepare_packed_git_one() Jeff King

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=53B191E6.7010509@web.de \
    --to=l.s.r@web.de \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=pclouds@gmail.com \
    --cc=peff@peff.net \
    /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).