From dfa0013583b1a0eef9c00e512fd6c48bbe7033a9 Mon Sep 17 00:00:00 2001
From: Denis Tingaikin <49399980+denis-tingajkin@users.noreply.github.com>
Date: Sun, 5 Jul 2020 15:32:00 +0700
Subject: [PATCH] Fix: goheader linter can throw nil pointer exception in case
 of a source file has not issues (#1209)

* fix potential nil pointer exception

Signed-off-by: denis-tingajkin <denis.tingajkin@xored.com>

* add test to cover

Signed-off-by: denis-tingajkin <denis.tingajkin@xored.com>
---
 pkg/golinters/goheader.go                        | 3 +++
 test/linters_test.go                             | 3 +++
 test/testdata/{go-header.go => go-header_bad.go} | 1 +
 test/testdata/go-header_good.go                  | 5 +++++
 4 files changed, 12 insertions(+)
 rename test/testdata/{go-header.go => go-header_bad.go} (99%)
 create mode 100644 test/testdata/go-header_good.go

diff --git a/pkg/golinters/goheader.go b/pkg/golinters/goheader.go
index 152069fa..8517e173 100644
--- a/pkg/golinters/goheader.go
+++ b/pkg/golinters/goheader.go
@@ -51,6 +51,9 @@ func NewGoHeader() *goanalysis.Linter {
 			var res []goanalysis.Issue
 			for _, file := range pass.Files {
 				i := a.Analyze(file)
+				if i == nil {
+					continue
+				}
 				issue := result.Issue{
 					Pos: token.Position{
 						Line:     i.Location().Line + 1,
diff --git a/test/linters_test.go b/test/linters_test.go
index ebe58ae8..191744a8 100644
--- a/test/linters_test.go
+++ b/test/linters_test.go
@@ -193,6 +193,9 @@ func extractRunContextFromComments(t *testing.T, sourcePath string) *runContext
 			skipMultilineComment(scanner)
 			continue
 		}
+		if strings.TrimSpace(line) == "" {
+			continue
+		}
 		if !strings.HasPrefix(line, "//") {
 			return rc
 		}
diff --git a/test/testdata/go-header.go b/test/testdata/go-header_bad.go
similarity index 99%
rename from test/testdata/go-header.go
rename to test/testdata/go-header_bad.go
index 6714a867..17c3099e 100644
--- a/test/testdata/go-header.go
+++ b/test/testdata/go-header_bad.go
@@ -1,4 +1,5 @@
 /*MY TITLE!*/ // ERROR "Expected:TITLE., Actual: TITLE!"
+
 //args: -Egoheader
 //config_path: testdata/configs/go-header.yml
 package testdata
diff --git a/test/testdata/go-header_good.go b/test/testdata/go-header_good.go
new file mode 100644
index 00000000..0e1b4241
--- /dev/null
+++ b/test/testdata/go-header_good.go
@@ -0,0 +1,5 @@
+/*MY TITLE.*/
+
+//args: -Egoheader
+//config_path: testdata/configs/go-header.yml
+package testdata