Skip to content

PatchApply panics with slice bounds out of rangeΒ #132

Description

@mwain

The following code panics when using PatchApply

func main() {
	dmp := diffmatchpatch.New()
	patches, _ := dmp.PatchFromText("@@ -1,2 +1,3 @@\n %E2%98%9E \n+r\n")
	fmt.Println(dmp.PatchToText(patches))

	s, _ := dmp.PatchApply(patches, "☞ 𝗒π—₯π——π—˜π—₯ ")
	fmt.Println(fmt.Sprintf("%q", s))
}
panic: runtime error: slice bounds out of range [:35] with length 33

goroutine 1 [running]:
github.com/sergi/go-diff/diffmatchpatch.(*DiffMatchPatch).PatchApply(0x14000109ef8, {0x14000078200?, 0x1, 0x1?}, {0x102d6650a, 0x19})
        /Users/michael/go/pkg/mod/github.com/sergi/go-diff@v1.2.0/diffmatchpatch/patch.go:306 +0x998
main.main()
        /Users/michael/code/playground/scratch/main.go:14 +0xf0
exit status 2

It looks like its finding the wrong start location here - https://github.com/sergi/go-diff/blob/master/diffmatchpatch/patch.go#L265
E.g: this prints 25...

func main() {
	dmp := diffmatchpatch.New()
	fmt.Println(dmp.MatchMain("\x01\x02\x03\x04☞ 𝗒π—₯π——π—˜π—₯ \x01\x02\x03\x04", "☞ \x01\x02\x03\x04", 4))
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions