Skip to content

Commit a384109

Browse files
authored
Prevent panic on diff generation (#16950) (#16951)
Backport #16950 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 #16943 Signed-off-by: Andrew Thornton <[email protected]>
1 parent 67ceb61 commit a384109

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

services/gitdiff/gitdiff.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,7 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio
996996

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

10011002
lineSectionInfo := getDiffLineSectionInfo(curFile.Name, line, leftLine-1, rightLine-1)
@@ -1036,6 +1037,7 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio
10361037
// Create a new section to represent this hunk
10371038
curSection = &DiffSection{}
10381039
curFile.Sections = append(curFile.Sections, curSection)
1040+
lastLeftIdx = -1
10391041
}
10401042
if lastLeftIdx > -1 {
10411043
diffLine.Match = lastLeftIdx
@@ -1061,6 +1063,7 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio
10611063
// Create a new section to represent this hunk
10621064
curSection = &DiffSection{}
10631065
curFile.Sections = append(curFile.Sections, curSection)
1066+
lastLeftIdx = -1
10641067
}
10651068
if len(curSection.Lines) == 0 || curSection.Lines[len(curSection.Lines)-1].Type != DiffLineDel {
10661069
lastLeftIdx = len(curSection.Lines)
@@ -1121,6 +1124,7 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio
11211124
curFile.IsBin = true
11221125
curFile.IsLFSFile = true
11231126
curSection.Lines = nil
1127+
lastLeftIdx = -1
11241128
}
11251129
}
11261130
}

0 commit comments

Comments
 (0)