Skip to content

Commit e34c4f6

Browse files
committed
rpm: add common version comparison function
Signed-off-by: Hank Donnay <hdonnay@redhat.com> Change-Id: I5f1f740f42b9ef6a8184a73873dbed56aebb5f6d
1 parent a18dc89 commit e34c4f6

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

internal/rpm/matcher.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package rpm
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/quay/claircore"
8+
"github.com/quay/claircore/internal/rpmver"
9+
)
10+
11+
// MatchVulnerable is a function implementing "driver.Matcher.Vulnerable"
12+
// in a common way.
13+
//
14+
// Given a package version "P" and vulnerability "V":
15+
//
16+
// - If a fixed version "F" is specified in "V", "P < F" is reported.
17+
// - If a package version "F" is specified in "V", "P <= F" is reported.
18+
// - If no version is provided in "V", this function compares against an
19+
// "infinite" version.
20+
//
21+
// In addition to this version comparison, the architectures are compared.
22+
func MatchVulnerable(ctx context.Context, rec *claircore.IndexRecord, vuln *claircore.Vulnerability) (bool, error) {
23+
p, err := rpmver.Parse(rec.Package.Version)
24+
if err != nil {
25+
return false, fmt.Errorf("rpm: unable to parse package version %q: %w",
26+
rec.Package.Version, err)
27+
}
28+
29+
var v rpmver.Version
30+
cmp := isLTE
31+
switch {
32+
case vuln.FixedInVersion != "":
33+
v, err = rpmver.Parse(vuln.FixedInVersion)
34+
cmp = isLT
35+
case vuln.Package.Version != "":
36+
v, err = rpmver.Parse(vuln.Package.Version)
37+
default:
38+
v, err = rpmver.Parse("65535:65535-65535")
39+
}
40+
if err != nil {
41+
return false, fmt.Errorf("rpm: unable to parse vulnerability version %q: %w",
42+
rec.Package.Version, err)
43+
}
44+
45+
return cmp(rpmver.Compare(&p, &v)) && vuln.ArchOperation.Cmp(rec.Package.Arch, vuln.Package.Arch), nil
46+
}
47+
48+
func isLTE(cmp int) bool { return cmp != 1 }
49+
func isLT(cmp int) bool { return cmp == -1 }

0 commit comments

Comments
 (0)