Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

edit_file and apply_patch corrupt every non-UTF-8 byte in the file

Closed
#159 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
77/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
typescript
Domain
desktop, tooling

Research direction

Start with src/main/tools/files.ts around edit_file, apply_patch.ts at the cited update paths, and write_file's overwrite path; inspect planPatch and the existing unit tests. Add a Windows-1252 fixture to verify non-UTF-8 bytes are refused without rewriting the file, then run the relevant tests. Done when all three write paths reject bytes that fail a UTF-8 round-trip.

Written by the indexing model from the issue text.

Description

bug priority: high severity: high

Found in the 2026-10-07 code review and confirmed with a probe against the real tools.

Problem

edit_file (src/main/tools/files.ts:246-253) and apply_patch (apply_patch.ts:203,267) decode the whole file with readFile(file, 'utf8') and write it back as UTF-8. isBinaryFile (text_files.ts) only checks for NUL bytes, so Windows-1252, Latin-1 and Shift-JIS files pass. Every byte that isn't valid UTF-8 becomes EF BF BD, anywhere in the file, not just in the edited region. The approval diff compares the decoded strings, so it shows only the edited hunk.

Scenario

A legacy .properties file contains café in Windows-1252. The agent edits an unrelated line. The user approves a clean-looking diff, and é is replaced. The probe caf\xe9\n… came out containing ef bf bd.

Fix

Refuse to edit when the bytes don't round-trip:

const bytes = await readFile(file);
const before = bytes.toString('utf8');
if (!Buffer.from(before, 'utf8').equals(bytes))
  throw new ToolError(`${input.path} is not UTF-8; editing it would rewrite other bytes.`);

Add the same check in planPatch for updates and in write_file when it overwrites an existing file. Optionally mention the encoding in read_file's result. Add unit tests with a Windows-1252 fixture.

Dominant language
TypeScript
Stars
2
Forks
2
Avg merge
5h 28m
Merged PRs (30d)
24

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from PierrunoYT/patch

All issues in PierrunoYT/patch

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.