sergi / go-diff

Diff, match and patch text in Go
MIT License
1.81k stars 207 forks source link

Index out of range error, would like help investigating #117

Closed noxora closed 3 years ago

noxora commented 3 years ago

It might be better for me to bring this to go-git, but I'm unsure, so I figured I'd come here first

panic: runtime error: index out of range

goroutine 1 [running]:
github.com/sergi/go-diff/diffmatchpatch.(*DiffMatchPatch).DiffCharsToLines(0xc004dbb648, 0xc002ef00c0, 0x1, 0x2, 0xc007
        /Users/{me}/go/pkg/mod/github.com/sergi/go-diff@v1.0.0/diffmatchpatch/diff.go:414 +0x31d
gopkg.in/src-d/go-git.v4/utils/diff.Do(0x0, 0x0, 0xc007276000, 0x9774e, 0x0, 0x0, 0x866171)
        /Users/{me}/go/pkg/mod/gopkg.in/src-d/go-git.v4@v4.10.0/utils/diff/diff.go:21 +0x1b5
gopkg.in/src-d/go-git.v4/plumbing/object.filePatchWithContext(0x8f4f20, 0xc000026048, 0xc00303e700, 0x0, 0x0, 0x7e75e0,
        /Users/{me}/go/pkg/mod/gopkg.in/src-d/go-git.v4@v4.10.0/plumbing/object/patch.go:68 +0x21b
gopkg.in/src-d/go-git.v4/plumbing/object.getPatchContext(0x8f4f20, 0xc000026048, 0x0, 0x0, 0xc004dbba30, 0x1, 0x1, 0x0,
        /Users/{me}/go/pkg/mod/gopkg.in/src-d/go-git.v4@v4.10.0/plumbing/object/patch.go:38 +0xd2
gopkg.in/src-d/go-git.v4/plumbing/object.(*Change).PatchContext(...)
        /Users/{me}/go/pkg/mod/gopkg.in/src-d/go-git.v4@v4.10.0/plumbing/object/change.go:93
gopkg.in/src-d/go-git.v4/plumbing/object.(*Change).Patch(...)
        /Users/{me}/go/pkg/mod/gopkg.in/src-d/go-git.v4@v4.10.0/plumbing/object/change.go:85
main.main()
        /Users/{me}/go/src/{my employer}/{my project}/main.go:152 +0x869

The above stack trace was the result of calling change.Patch() in go-git, which eventually calls y'all. The function is here, and you can switch to the master branch to step through the functions https://github.com/go-git/go-git/blob/db6c41c156481962abf9a55a324858674c25ab08/plumbing/object/change.go#L84 and I am confused because it seems that everything in both go-git and go-diff are using for loops on their input, so an empty input should just result in nothing happening and moving on.

My project is a git hook trying to get some info about the additions being made to the repository, so it takes a list of git references, attempts to get the patches which lead from reference to the other, and get its info from the added text in those patches.

I have been told that the repository this failed on was previously a mirrored repository, which had its mirroring turned off after they decided to migrate to it. Additionally, the repository had it's master branch history rewritten once, but I don't believe either of these necessarily create this issue, although they make it hard to replicate.

It's worth noting that this is currently running on go-git 4.10.0 and go-diff 1.0.0, which I know are not the latest versions, but I have manually compared the versions as best I can, and the code surrounding this error doesn't seem to have changed.

I would really appreciate help diagnosing this issue, and if there's any way I can help, please let me know, although I feel stuck at the moment

atrakh commented 3 years ago

Hi, we've also noticed this issue on go-diff@v1.1.0 paired with go-git/v5@v5.1.0: https://github.com/launchdarkly/ld-find-code-refs/issues/140

It looks like some changes were recently committed to potentially resolve this issue -- @sergi any chance you could release a new tag for the latest version so we could send it downstream?

Ortega-Dan commented 3 years ago

Hi. Is there going to be a new release, as asked before ?

sergi commented 3 years ago

v1.2.0 has been released, apologies for the delay.