Skip to content

Commit 3202072

Browse files
committed
fix panic: runtime error: slice bounds out of range
1 parent 849d7eb commit 3202072

File tree

3 files changed

+33
-26
lines changed

3 files changed

+33
-26
lines changed

diffmatchpatch/diff.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ const (
3434
DiffInsert Operation = 1
3535
// DiffEqual item represents an equal diff.
3636
DiffEqual Operation = 0
37-
//IndexSeparator is used to seperate the array indexes in an index string
38-
IndexSeparator = ","
3937
)
4038

4139
// Diff represents one diff operation
@@ -406,17 +404,11 @@ func (dmp *DiffMatchPatch) DiffLinesToRunes(text1, text2 string) ([]rune, []rune
406404
func (dmp *DiffMatchPatch) DiffCharsToLines(diffs []Diff, lineArray []string) []Diff {
407405
hydrated := make([]Diff, 0, len(diffs))
408406
for _, aDiff := range diffs {
409-
chars := strings.Split(aDiff.Text, IndexSeparator)
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-
}
407+
var sb strings.Builder
408+
for _, i := range []rune(aDiff.Text) {
409+
sb.WriteString(lineArray[i])
417410
}
418-
419-
aDiff.Text = strings.Join(text, "")
411+
aDiff.Text = sb.String()
420412
hydrated = append(hydrated, aDiff)
421413
}
422414
return hydrated

diffmatchpatch/patch_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,3 +337,28 @@ func TestPatchApply(t *testing.T) {
337337
assert.Equal(t, tc.ExpectedApplies, actualApplies, fmt.Sprintf("Test case #%d, %s", i, tc.Name))
338338
}
339339
}
340+
341+
func TestPatchMakeOutOfRangePanic(t *testing.T) {
342+
text1 := `
343+
1111111111111 000000
344+
------------- ------
345+
xxxxxxxxxxxxx ------
346+
xxxxxxxxxxxxx ------
347+
xxxxxxxxxxxxx xxxxxx
348+
xxxxxxxxxxxxx ......
349+
xxxxxxxxxxxxx 111111
350+
xxxxxxxxxxxxx ??????
351+
xxxxxxxxxxxxx 333333
352+
xxxxxxxxxxxxx 555555
353+
xxxxxxxxxx xxxxx
354+
xxxxxxxxxx xxxxx
355+
xxxxxxxxxx xxxxx
356+
xxxxxxxxxx xxxxx
357+
`
358+
text2 := `
359+
2222222222222 000000
360+
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx`
361+
dmp := New()
362+
patches := dmp.PatchMake(text1, text2)
363+
assert.Equal(t, 6, len(patches), "TestPatchMakeOutOfRangePanic")
364+
}

diffmatchpatch/stringutil.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
package diffmatchpatch
1010

1111
import (
12-
"strconv"
1312
"strings"
1413
"unicode/utf8"
1514
)
@@ -89,18 +88,9 @@ func runesIndex(r1, r2 []rune) int {
8988
}
9089

9190
func intArrayToString(ns []uint32) string {
92-
if len(ns) == 0 {
93-
return ""
91+
runes := make([]rune, len(ns))
92+
for i := 0; i < len(ns); i++ {
93+
runes[i] = rune(ns[i])
9494
}
95-
96-
indexSeparator := IndexSeparator[0]
97-
98-
// Appr. 3 chars per num plus the comma.
99-
b := []byte{}
100-
for _, n := range ns {
101-
b = strconv.AppendInt(b, int64(n), 10)
102-
b = append(b, indexSeparator)
103-
}
104-
b = b[:len(b)-1]
105-
return string(b)
95+
return string(runes)
10696
}

0 commit comments

Comments
 (0)