Describe the bug
For a specifically crafted input, diffFromDelta would produce a delta that cause diffCleanupSemantic and diffCleanupSemanticLossless to hang (infinite loop).
The documentation for diffFromDelta says that it would throw on invalid input, but it's not doing that for this case. Or this could be an issue with diffCleanupSemantic and diffCleanupSemanticLossless where they are mishandling specific diffs.
This is happening due to the empty edit/equality tuples. Reproducible on the latest v2.0.1.
Reproduction:
import { diffCleanupSemantic, diffFromDelta } from 'diff-match-patch-es'
const diffs = diffFromDelta('ac', '=1\t-0\t=0\t+b\t=1')
console.log('diff:', JSON.stringify(diffs))
diffCleanupSemantic(diffs)
Potential fix (unverified):
The original Java version has some extra check:
https://github.com/google/diff-match-patch/blob/62f2e689f498f9c92dbc588c58750addec9b1654/java/src/name/fraser/neil/plaintext/diff_match_patch.java#L938-L939
while (edit.length() != 0 && equality2.length() != 0
&& edit.charAt(0) == equality2.charAt(0)) {
But missing from the JS (and TS) version:
|
while (edit.charAt(0) === equality2.charAt(0)) { |
Reproduction
https://github.com/aforemendude/bugs-reproduction/tree/main/diff-match-patch-es-delta-inf-loop
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.39 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
Contributions
Describe the bug
For a specifically crafted input,
diffFromDeltawould produce a delta that causediffCleanupSemanticanddiffCleanupSemanticLosslessto hang (infinite loop).The documentation for
diffFromDeltasays that it would throw on invalid input, but it's not doing that for this case. Or this could be an issue withdiffCleanupSemanticanddiffCleanupSemanticLosslesswhere they are mishandling specific diffs.This is happening due to the empty edit/equality tuples. Reproducible on the latest
v2.0.1.Reproduction:
Potential fix (unverified):
The original Java version has some extra check:
https://github.com/google/diff-match-patch/blob/62f2e689f498f9c92dbc588c58750addec9b1654/java/src/name/fraser/neil/plaintext/diff_match_patch.java#L938-L939
But missing from the JS (and TS) version:
diff-match-patch-es/src/diff.ts
Line 880 in 4f35fb7
Reproduction
https://github.com/aforemendude/bugs-reproduction/tree/main/diff-match-patch-es-delta-inf-loop
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.39 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/npmUsed Package Manager
npm
Validations
Contributions