Skip to content

Commit 2705696

Browse files
zeripath6543
andauthored
Prevent NPE in CommentMustAsDiff if no hunk header (#15199) (#15200)
Backport #15199 I do not understand how this can happen or why. There is an apparent possibility for a comment.Patch to be missing a hunk header - this should not happen and do not understand how. But it appears to happen on 1.13 at least in some case. This PR will simply add a new section if the cursection is empty thus preventing the NPE. Fix #15198 Signed-off-by: Andrew Thornton <[email protected]> Co-authored-by: 6543 <[email protected]>
1 parent 2b68f66 commit 2705696

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

services/gitdiff/gitdiff.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,11 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio
10141014
}
10151015
diffLine := &DiffLine{Type: DiffLineAdd, RightIdx: rightLine}
10161016
rightLine++
1017+
if curSection == nil {
1018+
// Create a new section to represent this hunk
1019+
curSection = &DiffSection{}
1020+
curFile.Sections = append(curFile.Sections, curSection)
1021+
}
10171022
curSection.Lines = append(curSection.Lines, diffLine)
10181023
case '-':
10191024
curFileLinesCount++
@@ -1026,6 +1031,11 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio
10261031
if leftLine > 0 {
10271032
leftLine++
10281033
}
1034+
if curSection == nil {
1035+
// Create a new section to represent this hunk
1036+
curSection = &DiffSection{}
1037+
curFile.Sections = append(curFile.Sections, curSection)
1038+
}
10291039
curSection.Lines = append(curSection.Lines, diffLine)
10301040
case ' ':
10311041
curFileLinesCount++
@@ -1036,6 +1046,11 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio
10361046
diffLine := &DiffLine{Type: DiffLinePlain, LeftIdx: leftLine, RightIdx: rightLine}
10371047
leftLine++
10381048
rightLine++
1049+
if curSection == nil {
1050+
// Create a new section to represent this hunk
1051+
curSection = &DiffSection{}
1052+
curFile.Sections = append(curFile.Sections, curSection)
1053+
}
10391054
curSection.Lines = append(curSection.Lines, diffLine)
10401055
default:
10411056
// This is unexpected

0 commit comments

Comments
 (0)