diff options
author | zeripath | 2021-04-01 19:30:44 +0100 |
---|---|---|
committer | GitHub | 2021-04-01 14:30:44 -0400 |
commit | 1062931cf19462cf5ff8d4f14eeead6c6fc58173 (patch) | |
tree | 1c8e33e2900ae297eada41bc1e96d04822465873 | |
parent | 8d4f8ebf3105811f9d4fe6338f32ecc5c0b76452 (diff) |
Prevent NPE in CommentMustAsDiff if no hunk header (#1519) (#15201)
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>
-rw-r--r-- | services/gitdiff/gitdiff.go | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go index 0d95817a4..aeb70bf68 100644 --- a/services/gitdiff/gitdiff.go +++ b/services/gitdiff/gitdiff.go @@ -1014,6 +1014,11 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio } diffLine := &DiffLine{Type: DiffLineAdd, RightIdx: 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) case '-': curFileLinesCount++ @@ -1026,6 +1031,11 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio if leftLine > 0 { 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) case ' ': curFileLinesCount++ @@ -1036,6 +1046,11 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio diffLine := &DiffLine{Type: DiffLinePlain, LeftIdx: leftLine, RightIdx: rightLine} leftLine++ 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) default: // This is unexpected |