Skip to content

Commit 4f57d03

Browse files
authored
fix: normalize uppercase V in semantic version comparison (#3461)
Signed-off-by: immanuwell <pchpr.00@list.ru>
1 parent 82044d5 commit 4f57d03

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

grype/version/semantic_version.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type semanticVersion struct {
1919
obj *hashiVer.Version
2020
}
2121

22-
var versionStartsWithV = regexp.MustCompile(`^v\d+`)
22+
var versionStartsWithV = regexp.MustCompile(`^[vV]\d+`)
2323

2424
func newSemanticVersion(raw string, strict bool) (semanticVersion, error) {
2525
clean := semverPrereleaseNormalizer.Replace(raw)
@@ -29,10 +29,11 @@ func newSemanticVersion(raw string, strict bool) (semanticVersion, error) {
2929
if strict {
3030
// we still want v-prefix processing
3131
if versionStartsWithV.MatchString(clean) {
32-
clean = strings.TrimPrefix(clean, "v")
32+
clean = trimLeadingV(clean)
3333
}
3434
verObj, err = hashiVer.NewSemver(clean)
3535
} else {
36+
clean = trimLeadingV(clean)
3637
verObj, err = hashiVer.NewVersion(clean)
3738
}
3839
if err != nil {

grype/version/semantic_version_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func TestSemanticVersion_Constraint(t *testing.T) {
2020
{version: "0.6.0", constraint: "> 0.1.0, < 0.5.0 || > 1.0.0, < 2.0.0", satisfied: false},
2121
{version: "2.5.0", constraint: "> 0.1.0, < 0.5.0 || > 1.0.0, < 2.0.0", satisfied: false},
2222
{version: "2.3.1", constraint: "2.3.1", satisfied: true},
23+
{version: "1.0.0", constraint: "= V1.0.0", satisfied: true},
2324
{version: "2.3.1", constraint: "= 2.3.1", satisfied: true},
2425
{version: "2.3.1", constraint: " = 2.3.1", satisfied: true},
2526
{version: "2.3.1", constraint: ">= 2.3.1", satisfied: true},
@@ -227,6 +228,13 @@ func TestSemanticVersion_Compare_Format(t *testing.T) {
227228
otherFormat: SemanticFormat,
228229
expectError: false,
229230
},
231+
{
232+
name: "same format successful comparison with uppercase V prefix",
233+
thisVersion: "V1.2.3",
234+
otherVersion: "1.2.3",
235+
otherFormat: SemanticFormat,
236+
expectError: false,
237+
},
230238
{
231239
name: "same format successful comparison with prerelease",
232240
thisVersion: "1.2.3-alpha",

0 commit comments

Comments
 (0)