Browse Source

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 <art27@cantab.net>

Co-authored-by: 6543 <6543@obermui.de>
tags/v1.14.0
zeripath 3 years ago
parent
commit
2705696d4d
No account linked to committer's email address
1 changed files with 15 additions and 0 deletions
  1. 15
    0
      services/gitdiff/gitdiff.go

+ 15
- 0
services/gitdiff/gitdiff.go View File

} }
diffLine := &DiffLine{Type: DiffLineAdd, RightIdx: rightLine} diffLine := &DiffLine{Type: DiffLineAdd, RightIdx: rightLine}
rightLine++ rightLine++
if curSection == nil {
// Create a new section to represent this hunk
curSection = &DiffSection{}
curFile.Sections = append(curFile.Sections, curSection)
}
curSection.Lines = append(curSection.Lines, diffLine) curSection.Lines = append(curSection.Lines, diffLine)
case '-': case '-':
curFileLinesCount++ curFileLinesCount++
if leftLine > 0 { if leftLine > 0 {
leftLine++ leftLine++
} }
if curSection == nil {
// Create a new section to represent this hunk
curSection = &DiffSection{}
curFile.Sections = append(curFile.Sections, curSection)
}
curSection.Lines = append(curSection.Lines, diffLine) curSection.Lines = append(curSection.Lines, diffLine)
case ' ': case ' ':
curFileLinesCount++ curFileLinesCount++
diffLine := &DiffLine{Type: DiffLinePlain, LeftIdx: leftLine, RightIdx: rightLine} diffLine := &DiffLine{Type: DiffLinePlain, LeftIdx: leftLine, RightIdx: rightLine}
leftLine++ leftLine++
rightLine++ rightLine++
if curSection == nil {
// Create a new section to represent this hunk
curSection = &DiffSection{}
curFile.Sections = append(curFile.Sections, curSection)
}
curSection.Lines = append(curSection.Lines, diffLine) curSection.Lines = append(curSection.Lines, diffLine)
default: default:
// This is unexpected // This is unexpected

Loading…
Cancel
Save