Skip to content

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

Closed
wants to merge 1 commit into from

Conversation

abelcha
Copy link

@abelcha abelcha commented May 20, 2025

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:

  • 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

Fixes the issue with circular module references in eslint.config.js files.

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
@Copilot Copilot AI review requested due to automatic review settings May 20, 2025 14:50
@abelcha
Copy link
Author

abelcha commented May 20, 2025

@microsoft-github-policy-service agree

@abelcha
Copy link
Author

abelcha commented May 20, 2025

@abelcha please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.

@microsoft-github-policy-service agree [company="{your company}"]

Options:

  • (default - no company specified) I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer.
@microsoft-github-policy-service agree
  • (when company given) I am making Submissions in the course of work for my employer (or my employer has intellectual property rights in my Submissions by contract or applicable law). I have permission from my employer to make Submissions and enter into this Agreement on behalf of my employer. By signing below, the defined term “You” includes me and my employer.
@microsoft-github-policy-service agree company="Microsoft"

Contributor License Agreement

@microsoft-github-policy-service agree

Copy link
Contributor

@Copilot Copilot AI left a 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 exceeds len(Text). Add a unit test for StarIndex > 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 imported fs package (io/fs). Consider renaming to vfs or fileSystem 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{

Comment on lines +42 to +43
// If StarIndex is at or beyond the end, we only need to check the prefix
return strings.HasPrefix(candidate, p.Text[:p.StarIndex])
Copy link
Preview

Copilot AI May 20, 2025

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.

Suggested change
// 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.

Copy link
Member

@DanielRosenwasser DanielRosenwasser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this was attempting to fix #870 but #875 already did that. Any details on what else this might have fixed?

Comment on lines +46 to +49
pattern := Pattern{
Text: "",
StarIndex: 0,
}
Copy link
Member

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).

@jakebailey
Copy link
Member

I think #875 fixed this

@jakebailey jakebailey closed this May 22, 2025
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.

3 participants