Skip to content
This repository was archived by the owner on Dec 1, 2024. It is now read-only.

Commit ec45a79

Browse files
skogtwinfiorix
authored andcommitted
Fix two bugs:
- nvdjson.smartVerCmp was comparing the full version strings instead of major, minor, path etc. substrings; - off-by-one error in AttributeColumnMap.CPE()
1 parent 206aa1b commit ec45a79

File tree

4 files changed

+53
-27
lines changed

4 files changed

+53
-27
lines changed

cmd/csv2cpe/csv2cpe.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ func (acm *AttributeColumnMap) CPE(cols []string, lower bool) (string, error) {
180180
for i, v := range m {
181181
j := i - 1
182182

183-
if len(cols) < j {
183+
if j >= len(cols) {
184184
continue
185185
}
186186

cmd/csv2cpe/csv2cpe_test.go

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package main
1717
import (
1818
"bytes"
1919
"flag"
20+
"fmt"
2021
"reflect"
2122
"testing"
2223
)
@@ -74,37 +75,59 @@ func TestRemoveColumns(t *testing.T) {
7475
}
7576

7677
func TestProcessor(t *testing.T) {
77-
fs := flag.NewFlagSet("test", flag.ContinueOnError)
78+
cases := []struct {
79+
flags []string
80+
skips IntSet
81+
in string
82+
out string
83+
}{
84+
{
85+
[]string{"-cpe_product=1", "-cpe_version=2"},
86+
NewIntSet(1, 2, 3),
87+
"Foo\t1.0...\tdelet\ta\nbar\t2.0\tdelet\tb",
88+
"a,cpe:/::foo:1.0\nb,cpe:/::bar:2.0\n",
89+
},
90+
{
91+
[]string{"-cpe_part=1", "-cpe_product=2", "-cpe_product=4"},
92+
NewIntSet(1, 2, 3),
93+
"a\tb\tc\n",
94+
"cpe:/a\n",
95+
},
96+
}
7897

79-
acm := &AttributeColumnMap{}
80-
acm.AddFlags(fs)
98+
for n, c := range cases {
99+
t.Run(fmt.Sprintf("case_%d", n), func(t *testing.T) {
100+
fs := flag.NewFlagSet("test", flag.ContinueOnError)
81101

82-
err := fs.Parse([]string{"-cpe_product=1", "-cpe_version=2"})
83-
if err != nil {
84-
t.Fatal(err)
85-
}
102+
acm := &AttributeColumnMap{}
103+
acm.AddFlags(fs)
86104

87-
var stdin, stdout bytes.Buffer
105+
err := fs.Parse(c.flags)
106+
if err != nil {
107+
t.Fatal(err)
108+
}
88109

89-
p := &Processor{
90-
InputComma: rune('\t'),
91-
OutputComma: rune(','),
92-
CPEToLower: true,
93-
CPEOutputColumn: 2,
94-
EraseInputColumns: NewIntSet(1, 2, 3),
95-
}
110+
var stdin, stdout bytes.Buffer
96111

97-
stdin.Write([]byte("Foo\t1.0...\tdelet\ta\nbar\t2.0\tdelet\tb"))
112+
p := &Processor{
113+
InputComma: rune('\t'),
114+
OutputComma: rune(','),
115+
CPEToLower: true,
116+
CPEOutputColumn: 2,
117+
EraseInputColumns: c.skips,
118+
}
98119

99-
err = p.Process(acm, &stdin, &stdout)
100-
if err != nil {
101-
t.Fatal(err)
102-
}
120+
stdin.Write([]byte(c.in))
103121

104-
have := stdout.String()
105-
want := "a,cpe:/::foo:1.0\nb,cpe:/::bar:2.0\n"
122+
err = p.Process(acm, &stdin, &stdout)
123+
if err != nil {
124+
t.Fatal(err)
125+
}
106126

107-
if have != want {
108-
t.Fatalf("unexpected output:\nwant: %q\nhave: %q\n", want, have)
127+
if out := stdout.String(); out != c.out {
128+
t.Fatalf("unexpected output:\nwant: %q\nhave: %q\n", c.out, out)
129+
}
130+
})
109131
}
132+
110133
}

cvefeed/internal/nvdjson/interfaces.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ func node2CPE(node *NVDCVEFeedJSON10DefCPEMatch) (*wfn.Attributes, error) {
147147
// Returns -1 if v1 < v2, 1 if v1 > v2 and 0 if v1 == v2.
148148
func smartVerCmp(v1, v2 string) int {
149149
for s1, s2 := v1, v2; len(s1) > 0 && len(s2) > 0; {
150-
num1, alpha1, skip1 := parseVerParts(v1)
151-
num2, alpha2, skip2 := parseVerParts(v2)
150+
num1, alpha1, skip1 := parseVerParts(s1)
151+
num2, alpha2, skip2 := parseVerParts(s2)
152152
if num1 > num2 {
153153
return 1
154154
}
@@ -183,6 +183,8 @@ func parseVerParts(v string) (num int, alpha string, skip int) {
183183
skip = strings.IndexRune(v, '.')
184184
if skip == -1 {
185185
skip = len(v)
186+
} else {
187+
skip++
186188
}
187189
}
188190
return num, v[alphaAt:skip], skip

cvefeed/internal/nvdjson/smartvercmp_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func TestSmartVerCmp(t *testing.T) {
2929
{"1.0.14", "1.0.4", 1},
3030
{"95SE", "98SP1", -1},
3131
{"16.0.0", "3.2.7", 1},
32+
{"10.23", "10.21", 1},
3233
}
3334
for _, c := range cases {
3435
t.Run(fmt.Sprintf("%q vs %q", c.v1, c.v2), func(t *testing.T) {

0 commit comments

Comments
 (0)