Skip to content

Commit f6725a1

Browse files
r-paisergi
authored andcommitted
Index out of range panic in DiffCharsToLines on large JSON diff
1 parent db1b095 commit f6725a1

File tree

2 files changed

+17
-27
lines changed

2 files changed

+17
-27
lines changed

diffmatchpatch/diff.go

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func (dmp *DiffMatchPatch) diffCompute(text1, text2 []rune, checklines bool, dea
195195
// diffLineMode does a quick line-level diff on both []runes, then rediff the parts for greater accuracy. This speedup can produce non-minimal diffs.
196196
func (dmp *DiffMatchPatch) diffLineMode(text1, text2 []rune, deadline time.Time) []Diff {
197197
// Scan the text on a line-by-line basis first.
198-
text1, text2, linearray := dmp.diffLinesToRunes(text1, text2)
198+
text1, text2, linearray := dmp.DiffLinesToRunes(string(text1), string(text2))
199199

200200
diffs := dmp.diffMainRunes(text1, text2, false, deadline)
201201

@@ -402,13 +402,23 @@ func (dmp *DiffMatchPatch) DiffLinesToRunes(text1, text2 string) ([]rune, []rune
402402
return []rune(chars1), []rune(chars2), lineArray
403403
}
404404

405-
func (dmp *DiffMatchPatch) diffLinesToRunes(text1, text2 []rune) ([]rune, []rune, []string) {
406-
return dmp.DiffLinesToRunes(string(text1), string(text2))
407-
}
408-
409405
// DiffCharsToLines rehydrates the text in a diff from a string of line hashes to real lines of text.
410406
func (dmp *DiffMatchPatch) DiffCharsToLines(diffs []Diff, lineArray []string) []Diff {
411-
hydrated := dmp.DiffStringsToLines(diffs, lineArray)
407+
hydrated := make([]Diff, 0, len(diffs))
408+
for _, aDiff := range diffs {
409+
chars := strings.Split(aDiff.Text, IndexSeperator)
410+
text := make([]string, len(chars))
411+
412+
for i, r := range chars {
413+
i1, err := strconv.Atoi(r)
414+
if err == nil {
415+
text[i] = lineArray[i1]
416+
}
417+
}
418+
419+
aDiff.Text = strings.Join(text, "")
420+
hydrated = append(hydrated, aDiff)
421+
}
412422
return hydrated
413423
}
414424

@@ -1345,23 +1355,3 @@ func (dmp *DiffMatchPatch) diffLinesToStringsMunge(text string, lineArray *[]str
13451355

13461356
return strings
13471357
}
1348-
1349-
// DiffStringsToLines rehydrates the text in a diff from a string of line hashes to real lines of text.
1350-
func (dmp *DiffMatchPatch) DiffStringsToLines(diffs []Diff, lineArray []string) []Diff {
1351-
hydrated := make([]Diff, 0, len(diffs))
1352-
for _, aDiff := range diffs {
1353-
chars := strings.Split(aDiff.Text, IndexSeperator)
1354-
text := make([]string, len(chars))
1355-
1356-
for i, r := range chars {
1357-
i1, err := strconv.Atoi(r)
1358-
if err == nil {
1359-
text[i] = lineArray[i1]
1360-
}
1361-
}
1362-
1363-
aDiff.Text = strings.Join(text, "")
1364-
hydrated = append(hydrated, aDiff)
1365-
}
1366-
return hydrated
1367-
}

diffmatchpatch/diff_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1511,7 +1511,7 @@ func BenchmarkDiffMainStringsLargeLines(b *testing.B) {
15111511
text1, text2, linearray := dmp.DiffLinesToStrings(s1, s2)
15121512

15131513
diffs := dmp.DiffMain(text1, text2, false)
1514-
diffs = dmp.DiffStringsToLines(diffs, linearray)
1514+
diffs = dmp.DiffCharsToLines(diffs, linearray)
15151515
}
15161516
}
15171517

0 commit comments

Comments
 (0)