Skip to content

fix(core): reject non-UTF-8 files instead of silently corrupting them - #45925

Closed
skyzhao1223 wants to merge 1 commit into
anomalyco:devfrom
skyzhao1223:fix/edit-nonutf8-corruption
Closed

skyzhao1223 wants to merge 1 commit into
anomalyco:devfrom
skyzhao1223:fix/edit-nonutf8-corruption

Conversation

@skyzhao1223

Copy link
Copy Markdown
Contributor

Issue for this PR

Closes #45924

Type of change

  • Bug fix

What does this PR do?

edit and apply_patch decoded file bytes with a non-fatal UTF-8 decoder, so invalid sequences (GBK/Latin-1 comments, stray bytes) were replaced with U+FFFD and the corrupted text was written back to disk. When oldString matched an ASCII region, the tool reported success while destroying every non-UTF-8 byte in the file.

The fix decodes with { fatal: true } and fails with an explicit file is not valid UTF-8 error, leaving the file untouched — matching the read tool's existing MalformedUtf8Error behavior.

How did you verify your code works?

Added regression tests in both suites writing a file with GBK bytes and asserting the tool returns an error and the file bytes are unchanged:

  • packages/core: bun test ./test/tool-edit.test.ts ./test/tool-apply-patch.test.ts (21 pass)
  • bun run typecheck clean, prettier clean

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

edit and apply_patch decoded file bytes with a non-fatal UTF-8 decoder,
replacing invalid sequences (GBK/Latin-1 comments, stray bytes) with
U+FFFD and then writing the corrupted text back to disk. Because the
replacement matched an ASCII region, the tool reported success while every
non-UTF-8 byte in the file was destroyed.

Decode with { fatal: true } and fail with an explicit 'file is not valid
UTF-8' error instead, matching the read tool's existing behavior. The file
is left untouched on failure.
@github-actions

Copy link
Copy Markdown
Contributor

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

edit and apply_patch silently corrupt non-UTF-8 files (U+FFFD replacement)

1 participant