From: Junio C Hamano <gitster@pobox.com>
To: Elijah Newren <newren@gmail.com>
Cc: Yannik Tausch <dev@ytausch.de>, git@vger.kernel.org
Subject: Re: [PATCH v2 1/2] dir: do not apply prefix to negative pathspecs
Date: Fri, 04 Sep 2026 07:21:15 -0700 [thread overview]
Message-ID: <xmqqmrtx6qsk.fsf@gitster.g> (raw)
In-Reply-To: <CABPp-BFJo80oE=rtWc0FRNUxVh=6NHZeQmHD2q69VGwDcrHNhw@mail.gmail.com> (Elijah Newren's message of "Thu, 3 Sep 2026 22:00:57 -0700")
Elijah Newren <newren@gmail.com> writes:
> Hi Yannik,
>
> On Thu, Sep 3, 2026 at 3:23 AM Yannik Tausch <dev@ytausch.de> wrote:
>>
>> common_prefix_len() derives the common prefix solely from positive
>> pathspecs, skipping those marked with PATHSPEC_EXCLUDE. However,
>> match_pathspec_with_flags() also passes that prefix when matching the
>> negative pathspecs.
>>
>> A negative pathspec may be shorter than the prefix. In that case,
>> match_pathspec_item() advances item->match beyond its allocation and
>> subtracts the prefix from item->len, producing a negative matchlen. It
>> then dereferences the out-of-bounds pointer. If the resulting byte is
>> not NUL, matchlen is converted to size_t when passed to ps_strncmp(),
>> which may cause a much larger out-of-bounds read.
>>
>> The problem can be reproduced with AddressSanitizer:
> ...
> Would it make sense to add a regression case whose failure before this
> patch is deterministic without ASan?
Very good point.
Even if a negative pathspec were long enough, it would produce an
incorrect result if you strip the leading part of a negative entry.
With positive elements "a/b" and "a/c", and a negative element
"x/b", both paths "a/b/m" and "a/c/n" should match the pathspec with
these three elements, but if you incorrectly use prefix=2 to strip
the common prefix computed across positives, i.e., "a/", while
trying to see if the path "a/b/m" matches negative "x/b", we'd end
up trying to see if subpath "b/m" (in "a/b/m", after 2 leading
prefix bytes are stripped away) matches subpattern "b" (in "x/b",
after incorrectly stripping 2 leading bytes). Yay, "b/m" begins
with "b" so it matches! Not quite.
$ git init
$ mkdir -p a/b a/c
$ >a/b/m >a/c/n
$ git add a
$ rungit jch ls-files a/b ':!x/b' a/c
a/b/m
a/c/n
$ rungit master ls-files a/b ':!x/b' a/c
a/c/n
So "if prefix computed across positives is longer than a negative
element" is a special case that may manifest as one extra breakage
(i.e., logically it is wrong in that it uses incorrectly shortened
pattern and path for negated matching and produce incorrect result,
but in addition to that, the negated pattern string points outside
the original string, accessing wrong piece of memory), but I tend to
agree that it is equally if not more important to demonstrate what
is broken even without that extra breakage.
Thanks.
next prev parent reply other threads:[~2026-09-04 14:21 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-02 13:04 [PATCH] dir: find common prefix among positive pathspecs Yannik Tausch
2026-09-02 17:07 ` Junio C Hamano
2026-09-03 9:59 ` Yannik Tausch
2026-09-03 10:02 ` [PATCH v2 0/2] dir: fix pathspec prefixes with exclusions Yannik Tausch
2026-09-03 10:03 ` [PATCH v2 1/2] dir: do not apply prefix to negative pathspecs Yannik Tausch
2026-09-04 5:00 ` Elijah Newren
2026-09-04 14:21 ` Junio C Hamano [this message]
2026-09-03 10:04 ` [PATCH v2 2/2] dir: find common prefix among non-exclude pathspec items Yannik Tausch
[not found] ` <CY5PR17MB6144A1A7BF2E101FE26A6A85B1B62@CY5PR17MB6144.namprd17.prod.outlook.com>
2026-09-03 11:49 ` Yannik Tausch
2026-09-03 18:11 ` Junio C Hamano
2026-09-03 18:13 ` pathspec: match and original in pathspec_item are const Junio C Hamano
2026-09-03 18:37 ` Yannik Tausch
2026-09-03 18:51 ` Junio C Hamano
2026-09-03 18:57 ` Yannik Tausch
2026-09-03 21:05 ` Junio C Hamano
2026-09-03 21:13 ` Yannik Tausch
2026-09-04 5:02 ` [PATCH v2 2/2] dir: find common prefix among non-exclude pathspec items Elijah Newren
2026-09-04 16:43 ` Junio C Hamano
2026-09-04 19:19 ` Elijah Newren
2026-09-05 16:14 ` Junio C Hamano
2026-09-03 18:06 ` [PATCH v2 0/2] dir: fix pathspec prefixes with exclusions Yannik Tausch
2026-09-03 18:43 ` [PATCH v3 0/3] " Yannik Tausch
2026-09-03 18:44 ` [PATCH v3 1/3] pathspec: match and original in pathspec_item are const Yannik Tausch
2026-09-03 18:45 ` [PATCH v3 2/3] dir: do not apply prefix to negative pathspecs Yannik Tausch
2026-09-03 18:45 ` [PATCH v3 3/3] dir: find common prefix among non-exclude pathspec items Yannik Tausch
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=xmqqmrtx6qsk.fsf@gitster.g \
--to=gitster@pobox.com \
--cc=dev@ytausch.de \
--cc=git@vger.kernel.org \
--cc=newren@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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox