From: Thomas Rast <trast@student.ethz.ch>
To: Junio C Hamano <gitster@pobox.com>
Cc: <git@vger.kernel.org>, Junio C Hamano <gitster@pobox.com>
Subject: [PATCH] common_prefix: simplify and fix scanning for prefixes
Date: Wed, 16 Jun 2010 01:02:03 +0200 [thread overview]
Message-ID: <f023050aede65c3cc6d9fdf0067c142dc0257c72.1276642610.git.trast@student.ethz.ch> (raw)
In-Reply-To: <7vaaqwgd2q.fsf@alter.siamese.dyndns.org>
From: Junio C Hamano <gitster@pobox.com>
common_prefix() scans backwards from the far end of each 'next'
pathspec, starting from 'len', shortening the 'prefix' using 'path' as
a reference.
However, there is a small opportunity for an out-of-bounds access
because len is unconditionally set to prefix-1 after a "direct match"
test failed. This means that if 'next' is shorter than prefix+2, we
read past it.
Instead of a minimal fix, simplify the loop: scan *forward* over the
'next' entry, remembering the last '/' where it matched the prefix
known so far. This is far easier to read and also has the advantage
that we only scan over each entry once.
Acked-by: Thomas Rast <trast@student.ethz.ch>
---
Junio C Hamano wrote:
> Thomas Rast <trast@student.ethz.ch> writes:
> > I really didn't like the two-interleaved-loops version in your last
> > mail, but this one is way more readable than even the original.
> >
> > (Why did you wrap the for loop? It's only 76 chars.)
>
> Because I was writing it in my MUA ;-)
Well then, perhaps I can at least repay the favour by suggesting a
commit message.
dir.c | 21 ++++++++-------------
1 files changed, 8 insertions(+), 13 deletions(-)
diff --git a/dir.c b/dir.c
index 5e36f8e..78eb869 100644
--- a/dir.c
+++ b/dir.c
@@ -33,20 +33,15 @@ static int common_prefix(const char **pathspec)
prefix = slash - path + 1;
while ((next = *++pathspec) != NULL) {
- int len = strlen(next);
- if (len >= prefix && !memcmp(path, next, prefix))
+ int len, last_matching_slash = -1;
+ for (len = 0; len < prefix && next[len] == path[len]; len++)
+ if (next[len] == '/')
+ last_matching_slash = len;
+ if (len == prefix)
continue;
- len = prefix - 1;
- for (;;) {
- if (!len)
- return 0;
- if (next[--len] != '/')
- continue;
- if (memcmp(path, next, len+1))
- continue;
- prefix = len + 1;
- break;
- }
+ if (last_matching_slash < 0)
+ return 0;
+ prefix = last_matching_slash + 1;
}
return prefix;
}
--
1.7.1.608.g80d39f
prev parent reply other threads:[~2010-06-15 23:02 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-06-10 18:24 [PATCH] common_prefix: be more careful about pathspec bounds Thomas Rast
2010-06-15 8:16 ` Thomas Rast
2010-06-15 9:05 ` Johannes Schindelin
2010-06-15 15:05 ` Junio C Hamano
2010-06-15 16:06 ` Junio C Hamano
2010-06-15 18:04 ` Thomas Rast
2010-06-15 22:12 ` Junio C Hamano
2010-06-15 23:02 ` Thomas Rast [this message]
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=f023050aede65c3cc6d9fdf0067c142dc0257c72.1276642610.git.trast@student.ethz.ch \
--to=trast@student.ethz.ch \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.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).