Skip to content

Prevent PatchMake panic on invalid UTF-8 - #158

Open
cneuralnetwork wants to merge 1 commit into
sergi:masterfrom
cneuralnetwork:agent/fix-invalid-utf8-patchmake
Open

Prevent PatchMake panic on invalid UTF-8#158
cneuralnetwork wants to merge 1 commit into
sergi:masterfrom
cneuralnetwork:agent/fix-invalid-utf8-patchmake

Conversation

@cneuralnetwork

Copy link
Copy Markdown

Summary

  • normalize an invalid-UTF-8 source only when it exactly matches the source reconstructed from DiffMain's diffs
  • preserve raw bytes for caller-supplied byte-level diffs
  • add regression coverage for both PatchMake(text1, text2) and PatchMake(text1, diffs)

Root cause

DiffMain converts input strings to []rune, replacing the one-byte invalid input with U+FFFD (three UTF-8 bytes). patchMake2 retained the original one-byte source, then used the three-byte delete length to slice it, causing slice bounds out of range [3:1].

The fix adopts the normalized source only when it is exactly the source described by the diffs. Handcrafted diffs that intentionally contain raw invalid bytes continue to use those bytes. This stays consistent with the invalid-UTF-8 behavior documented by DiffMain and does not introduce new byte-level diff semantics.

Validation

  • go test ./...
  • go test -race -count=1 -timeout 120s ./...
  • go vet ./...

Fixes #157

@cneuralnetwork
cneuralnetwork marked this pull request as ready for review August 2, 2026 15:48
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.

PatchMake panics for a one-byte invalid UTF-8 string

1 participant