fix(file): reject empty old string in StrReplaceFile - #2631
Open
rootkiller6788 wants to merge 2 commits into
Open
fix(file): reject empty old string in StrReplaceFile#2631rootkiller6788 wants to merge 2 commits into
rootkiller6788 wants to merge 2 commits into
Conversation
An empty `old` makes str.replace() insert `new` at the start or between every character instead of matching nothing, so a bad edit silently mangles the file. Validate the edit before applying so the tool errors out instead.
Adds cases for a plain empty old, replace_all, and an empty old buried in a list of edits. All should error out and leave the file untouched.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Found this while testing the StrReplaceFile tool with odd inputs. If the agent hands it an empty
oldstring, str.replace() doesn't match nothing — it insertsnewat the front (or between every character with replace_all=True), and the tool happily reports success. So a bad edit quietly mangles the file.Fix is to validate each edit up front and bail out with an error if
oldis empty, before anything is written. Added a few tests: empty old, empty old with replace_all, and an empty old buried in a list of edits. All error out and leave the file as-is.