Skip to content

Treat a '-' in a character group as a literal unless it is a range - #37

Open
compiler-segfault wants to merge 1 commit into
git-lfs:mainfrom
compiler-segfault:fix/char-class-range-marker
Open

compiler-segfault wants to merge 1 commit into
git-lfs:mainfrom
compiler-segfault:fix/char-class-range-marker

Conversation

@compiler-segfault

Copy link
Copy Markdown

parseComponent treats every - in a character group as a range marker, so a group whose content ends with - reads one byte past the end of the pattern:

$ git lfs track '[a-'
Tracking "[a-"
panic: runtime error: index out of range [3] with length 3
  github.com/git-lfs/wildmatch/v2.parseComponent (wildmatch.go:565)
  github.com/git-lfs/wildmatch/v2.(*Wildmatch).parseTokensSimple (wildmatch.go:230)
  github.com/git-lfs/wildmatch/v2.NewWildmatch (wildmatch.go:108)
  github.com/git-lfs/git-lfs/v3/filepathfilter.NewPattern (filepathfilter.go:194)
  ...

git lfs ls-files --include='[a-' crashes the same way on git-lfs v3.8.1, and so does [[:alpha:]-.

The same branch also builds the wrong range whenever the character after the - is not the second bound of a range:

pattern subject git check-ignore wildmatch v2.0.1
[a-] - match no match
[a-] ] no match match
[-a] ] no match match
[a-]x ax match no match
[!a-] - no match match
[a-c-e] d no match match

The git column comes from git check-ignore --no-index --stdin in a scratch repository.

This makes a - a range marker only when it sits between two characters of the group and treats it as a literal otherwise, consumes an escaped character that ends a range ([A-\]), and makes a group without a closing ] match nothing, which is what git does as well.

Tests cover the panic and every row of the table above.

A '-' is a range marker only when it sits between two characters of the
group.  A group whose content ended with '-' indexed one byte past the end
of the pattern and panicked with "index out of range"; git-lfs reaches this
through `git lfs track '[a-'` and `git lfs ls-files --include='[a-'`.

A '-' in the first or last position, or right after a range, is now a
literal, and the character which ends a range may be escaped.  A group
without a closing ']' now matches nothing, as it does in git.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant