Skip to content

fix(file): reject empty old string in StrReplaceFile - #2631

Open
rootkiller6788 wants to merge 2 commits into
MoonshotAI:mainfrom
rootkiller6788:fix-str-replace-empty-old
Open

fix(file): reject empty old string in StrReplaceFile#2631
rootkiller6788 wants to merge 2 commits into
MoonshotAI:mainfrom
rootkiller6788:fix-str-replace-empty-old

Conversation

@rootkiller6788

@rootkiller6788 rootkiller6788 commented Aug 31, 2026

Copy link
Copy Markdown

Found this while testing the StrReplaceFile tool with odd inputs. If the agent hands it an empty old string, str.replace() doesn't match nothing — it inserts new at 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 old is 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.


Devin Review

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.

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

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