Skip to content

Commit f9beae7

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

File tree

2 files changed

+11
-27
lines changed

2 files changed

+11
-27
lines changed

diffmatchpatch/diff.go

+11-12
Original file line numberDiff line numberDiff line change
@@ -392,13 +392,13 @@ func (dmp *DiffMatchPatch) diffBisectSplit(runes1, runes2 []rune, x, y int,
392392
// DiffLinesToChars splits two texts into a list of strings, and educes the texts to a string of hashes where each Unicode character represents one line.
393393
// It's slightly faster to call DiffLinesToRunes first, followed by DiffMainRunes.
394394
func (dmp *DiffMatchPatch) DiffLinesToChars(text1, text2 string) (string, string, []string) {
395-
chars1, chars2, lineArray := dmp.DiffLinesToRunes(text1, text2)
396-
return string(chars1), string(chars2), lineArray
395+
chars1, chars2, lineArray := dmp.diffLinesToStrings(text1, text2)
396+
return chars1, chars2, lineArray
397397
}
398398

399-
// DiffLinesToRunes splits two texts into a list of runes. Each rune represents one line.
399+
// DiffLinesToRunes splits two texts into a list of runes.
400400
func (dmp *DiffMatchPatch) DiffLinesToRunes(text1, text2 string) ([]rune, []rune, []string) {
401-
chars1, chars2, lineArray := dmp.DiffLinesToStrings(text1, text2)
401+
chars1, chars2, lineArray := dmp.diffLinesToStrings(text1, text2)
402402
return []rune(chars1), []rune(chars2), lineArray
403403
}
404404

@@ -1308,8 +1308,8 @@ func (dmp *DiffMatchPatch) DiffFromDelta(text1 string, delta string) (diffs []Di
13081308
return diffs, nil
13091309
}
13101310

1311-
// DiffLinesToStrings splits two texts into a list of strings. Each string represents one line.
1312-
func (dmp *DiffMatchPatch) DiffLinesToStrings(text1, text2 string) (string, string, []string) {
1311+
// diffLinesToStrings splits two texts into a list of strings. Each string represents one line.
1312+
func (dmp *DiffMatchPatch) diffLinesToStrings(text1, text2 string) (string, string, []string) {
13131313
// '\x00' is a valid character, but various debuggers don't like it. So we'll insert a junk entry to avoid generating a null character.
13141314
lineArray := []string{""} // e.g. lineArray[4] == 'Hello\n'
13151315

@@ -1324,14 +1324,13 @@ func (dmp *DiffMatchPatch) DiffLinesToStrings(text1, text2 string) (string, stri
13241324
return str1, str2, lineArray
13251325
}
13261326

1327-
// diffLinesToStringsMunge splits a text into an array of strings, and reduces the texts to a []rune where each Unicode character represents one line.
1328-
// We use strings instead of []runes as input mainly because you can't use []rune as a map key.
1327+
// diffLinesToStringsMunge splits a text into an array of strings, and reduces the texts to a []string.
13291328
func (dmp *DiffMatchPatch) diffLinesToStringsMunge(text string, lineArray *[]string) []string {
13301329
// Walk the text, pulling out a substring for each line. text.split('\n') would would temporarily double our memory footprint. Modifying text would create many large strings to garbage collect.
13311330
lineHash := map[string]int{} // e.g. lineHash['Hello\n'] == 4
13321331
lineStart := 0
13331332
lineEnd := -1
1334-
strings := []string{}
1333+
strs := []string{}
13351334

13361335
for lineEnd < len(text)-1 {
13371336
lineEnd = indexOf(text, "\n", lineStart)
@@ -1345,13 +1344,13 @@ func (dmp *DiffMatchPatch) diffLinesToStringsMunge(text string, lineArray *[]str
13451344
lineValue, ok := lineHash[line]
13461345

13471346
if ok {
1348-
strings = append(strings, strconv.Itoa(lineValue))
1347+
strs = append(strs, strconv.Itoa(lineValue))
13491348
} else {
13501349
*lineArray = append(*lineArray, line)
13511350
lineHash[line] = len(*lineArray) - 1
1352-
strings = append(strings, strconv.Itoa(len(*lineArray)-1))
1351+
strs = append(strs, strconv.Itoa(len(*lineArray)-1))
13531352
}
13541353
}
13551354

1356-
return strings
1355+
return strs
13571356
}

diffmatchpatch/diff_test.go

-15
Original file line numberDiff line numberDiff line change
@@ -1500,21 +1500,6 @@ func BenchmarkDiffMainRunesLargeLines(b *testing.B) {
15001500
}
15011501
}
15021502

1503-
func BenchmarkDiffMainStringsLargeLines(b *testing.B) {
1504-
s1, s2 := speedtestTexts()
1505-
1506-
dmp := New()
1507-
1508-
b.ResetTimer()
1509-
1510-
for i := 0; i < b.N; i++ {
1511-
text1, text2, linearray := dmp.DiffLinesToStrings(s1, s2)
1512-
1513-
diffs := dmp.DiffMain(text1, text2, false)
1514-
diffs = dmp.DiffCharsToLines(diffs, linearray)
1515-
}
1516-
}
1517-
15181503
func BenchmarkDiffMainRunesLargeDiffLines(b *testing.B) {
15191504
fp, _ := os.Open("../testdata/diff10klinestest.txt")
15201505
defer fp.Close()

0 commit comments

Comments
 (0)