Skip to content

Fix regular expression replacement with anchors causing infinite recursion #3028

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

braedonbillingsley
Copy link

Fixes an issue where using anchors (like ^) in "Replace All" operations would cause infinite loops, removing more text than intended.

When performing a regex "Replace All" with patterns containing anchors like ^ (start of line), the current implementation would recurse infinitely on the same line. For example:

Pattern: ^ (start of line + space)
Replace with: `` (empty string)
Expected: Remove one leading space from each line
Actual: Remove ALL leading spaces from each line

Root Cause: The replaceAll() method uses a loop that continues until no matches are found.

Solution:
Added position tracking to detect when the same position is matched repeatedly:

Track the last found position (lastFoundPosition)
If the same position is found consecutively, advance past it manually
This breaks the infinite loop while preserving normal replacement behavior

Code Changes:
File: bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/FindReplaceLogic.java
Method: replaceAll()

Changes:
Added lastFoundPosition tracking variable
Added position comparison logic to detect repeated matches
Force advance past problematic positions when detected

Fixes #2820

braedonbillingsley and others added 2 commits June 2, 2025 09:49
recursion

- Prevent infinite loops when using anchors like '^' in replace all
operations
- Track last found position to detect and skip repeated matches at same
location
- Resolves issue where '^ ' to '' removed all leading spaces instead of
one

Fixes eclipse-platform#2820
@merks
Copy link
Contributor

merks commented Jun 8, 2025

Please rebase and squash to a single commit. We cannot incorporate merge commits.

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.

Regular expression replacement with an anchor improperly recurses on each line
2 participants