-
Notifications
You must be signed in to change notification settings - Fork 650
Fix panic in Pattern.Matches with zero StarIndex #883
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This commit fixes a panic that occurs in the Pattern.Matches method when StarIndex is 0 and Text is empty or too short. The panic was caused by attempting to slice Text with invalid bounds [1:0]. The fix adds special handling for edge cases: - Empty Text - StarIndex at the beginning of Text - StarIndex at or beyond the end of Text Added tests to verify the fix: - Unit tests in internal/core/pattern_test.go - Integration test in internal/module/circular_reference_test.go - Compiler test case in testdata/tests/cases/compiler/circularModuleReference.ts
@microsoft-github-policy-service agree |
@microsoft-github-policy-service agree |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR addresses a panic in Pattern.Matches
when StarIndex
is 0 and Text
is empty or too short, and adds tests to prevent regressions in module resolution with circular references.
- Add edge‐case handling in
Pattern.Matches
for empty text, star at start, and star beyond end - Introduce unit and integration tests for zero‐star and circular module imports
- Include a compiler test case for circular
eslint.config.js
references
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
File | Description |
---|---|
internal/core/pattern.go | Added branches to safely handle empty text, StarIndex == 0 , and out‐of‐range stars |
internal/core/pattern_test.go | New unit tests for zero‐star matching and circular pattern scenarios |
internal/module/circular_reference_test.go | Integration test to verify no panic during circular module resolution |
testdata/tests/cases/compiler/circularModuleReference.ts | TS compiler test case reproducing circular import panic |
Comments suppressed due to low confidence (3)
internal/core/pattern.go:41
- There’s no existing test for a pattern where
StarIndex
exceedslen(Text)
. Add a unit test forStarIndex > len(Text)
to ensure the new branch handles it safely.
if p.StarIndex >= len(p.Text) {
internal/module/circular_reference_test.go:18
- [nitpick] The variable name
fs
shadows the importedfs
package (io/fs
). Consider renaming tovfs
orfileSystem
to avoid confusion.
fs := vfstest.FromMap(map[string]string{
internal/module/circular_reference_test.go:31
- [nitpick] This line and its preceding comment aren't indented to match the surrounding block. Align the indentation for readability.
pattern := core.Pattern{
// If StarIndex is at or beyond the end, we only need to check the prefix | ||
return strings.HasPrefix(candidate, p.Text[:p.StarIndex]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Slicing p.Text[:p.StarIndex]
will panic if StarIndex
> len(p.Text)
. Clamp the slice index to len(p.Text)
or explicitly handle the >
case so it never exceeds the text length.
// If StarIndex is at or beyond the end, we only need to check the prefix | |
return strings.HasPrefix(candidate, p.Text[:p.StarIndex]) | |
// If StarIndex is at or beyond the end, clamp it to len(p.Text) for safe slicing | |
return strings.HasPrefix(candidate, p.Text[:len(p.Text)]) |
Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pattern := Pattern{ | ||
Text: "", | ||
StarIndex: 0, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should never happen - the empty string is an exact match (StarIndex == -1
).
I think #875 fixed this |
This PR fixes a panic that occurs in the Pattern.Matches method when StarIndex is 0 and Text is empty or too short. The panic was caused by attempting to slice Text with invalid bounds [1:0].
The fix adds special handling for edge cases:
Added tests to verify the fix:
Fixes the issue with circular module references in eslint.config.js files.