Skip to content

Commit 2a95d61

Browse files
committed
Refactor error position parsing to support path with colons
1 parent cfb3c8f commit 2a95d61

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

analyzer.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"path"
3030
"path/filepath"
3131
"reflect"
32+
"regexp"
3233
"runtime/debug"
3334
"strconv"
3435
"strings"
@@ -761,19 +762,24 @@ func ParseErrors(pkg *packages.Package) (map[string][]Error, error) {
761762
return nil, nil
762763
}
763764
errs := make(map[string][]Error)
764-
var posRegexp = regexp.MustCompile(`^(.*?)(?::(\d+))?(?::(\d+))?$`)
765+
var posRegexp = regexp.MustCompile(`^(.*?)(?::(\w+))?(?::(\w+))?$`)
765766
for _, pkgErr := range pkg.Errors {
766767
matches := posRegexp.FindStringSubmatch(pkgErr.Pos)
767768
file := pkgErr.Pos
769+
var err error
768770
var line, column int
769771
if len(matches) > 0 {
770772
file = matches[1]
771773
file = strings.TrimSuffix(file, ":")
772774
if matches[2] != "" {
773-
line, _ = strconv.Atoi(matches[2])
775+
if line, err = strconv.Atoi(matches[2]); err != nil {
776+
return nil, fmt.Errorf("parsing line: %w", err)
777+
}
774778
}
775779
if matches[3] != "" {
776-
column, _ = strconv.Atoi(matches[3])
780+
if column, err = strconv.Atoi(matches[3]); err != nil {
781+
return nil, fmt.Errorf("parsing column: %w", err)
782+
}
777783
}
778784
}
779785
msg := strings.TrimSpace(pkgErr.Msg)

analyzer_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1803,6 +1803,26 @@ func main() {
18031803
Expect(err).Should(HaveOccurred())
18041804
})
18051805

1806+
It("should properly parse the errors with colons in path", func() {
1807+
pkg := &packages.Package{
1808+
Errors: []packages.Error{
1809+
{
1810+
Pos: "C:\\file:1:2",
1811+
Msg: "build error",
1812+
},
1813+
},
1814+
}
1815+
errors, err := gosec.ParseErrors(pkg)
1816+
Expect(err).ShouldNot(HaveOccurred())
1817+
Expect(errors).To(HaveLen(1))
1818+
for _, ferr := range errors {
1819+
Expect(ferr).To(HaveLen(1))
1820+
Expect(ferr[0].Line).To(Equal(1))
1821+
Expect(ferr[0].Column).To(Equal(2))
1822+
Expect(ferr[0].Err).Should(MatchRegexp(`build error`))
1823+
}
1824+
})
1825+
18061826
It("should append error to the same file", func() {
18071827
pkg := &packages.Package{
18081828
Errors: []packages.Error{

0 commit comments

Comments
 (0)