antfu / antfu/diff-match-patch-es

diffToDelta and patchToText can throw if diff split surrogate-pair

Open
#6 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
200
Forks
6
PR merge metrics
No merged PRs in 30d

Description

Describe the bug

The current diff implementation operates on UTF-16 characters directly, which can cause it to split surrogate-pairs. When this happens, diffToDelta and patchToText can throw URIError.

Reproducible on the latest v2.0.1.

Reproduction:

import { diff, diffToDelta, patch, patchToText } from 'diff-match-patch-es'

const oldText = '😀'
const newText = '😃'

const diffs = diff(oldText, newText)
console.log('diff:', JSON.stringify(diffs))

for (const [label, reproduce] of [
  ['diffToDelta', () => diffToDelta(diffs)],
  ['patchToText', () => patchToText(patch(oldText, newText))],
]) {
  try {
    console.log(`${label}:`, reproduce())
  } catch (error) {
    console.log(`${label}:`, `${error.name}: ${error.message}`)
  }
}

Output:

diff: [[0,"\ud83d"],[-1,"\ude00"],[1,"\ude03"]]
diffToDelta: URIError: URI malformed
patchToText: URIError: URI malformed
Reproduction

https://github.com/aforemendude/bugs-reproduction/tree/main/diff-match-patch-es-urierror

System Info
  System:
    OS: Linux 7.0 Ubuntu 24.04.4 LTS 24.04.4 LTS (Noble Numbat)
    CPU: (4) x64 Intel(R) N95
    Memory: 12.10 GB / 15.40 GB
    Container: Yes
    Shell: 5.2.21 - /bin/bash
  Binaries:
    Node: 24.18.0 - /usr/local/nodejs/bin/node
    npm: 11.16.0 - /usr/local/nodejs/bin/npm
Used Package Manager

npm

Validations
  • Follow our Code of Conduct
  • Read the Contributing Guide.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • Check that this is a concrete bug. For Q&A, please open a GitHub Discussion instead.
  • The provided reproduction is a minimal reproducible of the bug.
Contributions
  • I am willing to submit a PR to fix this issue
  • I am willing to submit a PR with failing tests (actually just go ahead and do it, thanks!)

Contributor guide

Open the contributing guide

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.

Research direction

Start with the diff, diffToDelta, patch, and patchToText entry points shown in the reproduction, then run the provided surrogate-pair example. Trace how the split characters reach the URI encoding step; done means the example completes without URIError and preserves the expected diff and patch text.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.