Skip to content

Commit

Permalink
Prevent panic on diff generation
Browse files Browse the repository at this point in the history
The lastLeftIdx should be reset at the same time as creating a new section otherwise
it is possible for a second addition to end up attempting to read a nil entry.

Fix go-gitea#16943

Signed-off-by: Andrew Thornton <art27@cantab.net>
  • Loading branch information
zeripath committed Sep 3, 2021
1 parent 7d1d32a commit 8aa4b37
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions services/gitdiff/gitdiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,7 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio

// Create a new section to represent this hunk
curSection = &DiffSection{}
lastLeftIdx = -1
curFile.Sections = append(curFile.Sections, curSection)

lineSectionInfo := getDiffLineSectionInfo(curFile.Name, line, leftLine-1, rightLine-1)
Expand Down Expand Up @@ -1036,6 +1037,7 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio
// Create a new section to represent this hunk
curSection = &DiffSection{}
curFile.Sections = append(curFile.Sections, curSection)
lastLeftIdx = -1
}
if lastLeftIdx > -1 {
diffLine.Match = lastLeftIdx
Expand All @@ -1061,6 +1063,7 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio
// Create a new section to represent this hunk
curSection = &DiffSection{}
curFile.Sections = append(curFile.Sections, curSection)
lastLeftIdx = -1
}
if len(curSection.Lines) == 0 || curSection.Lines[len(curSection.Lines)-1].Type != DiffLineDel {
lastLeftIdx = len(curSection.Lines)
Expand Down

0 comments on commit 8aa4b37

Please sign in to comment.