Skip to content

Commit ae4bc6f

Browse files
authored
fix(vulnfeeds): Improve semver sort comparison so that it is parts length agnostic (#3851)
When running the converter on some Linux vulns, it would break if parts lengths were different. This should make the comparison parts-length agnostic.
1 parent 8cfb69f commit ae4bc6f

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

vulnfeeds/cmd/cvelist2osv/version_extraction.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -423,19 +423,19 @@ func buildVersionRange(intro string, lastAff string, fixed string) osvschema.Ran
423423
func sortBadSemver(a, b string) int {
424424
partsA := strings.Split(a, ".")
425425
partsB := strings.Split(b, ".")
426-
majorA, _ := strconv.Atoi(partsA[0])
427-
majorB, _ := strconv.Atoi(partsB[0])
428426

429-
if c := cmp.Compare(majorA, majorB); c != 0 {
430-
return c
427+
minLen := min(len(partsA), len(partsB))
428+
for i := 0; i < minLen; i++ {
429+
// Convert parts to integers for numerical comparison.
430+
// We ignore the error, so non-numeric parts default to 0.
431+
numA, _ := strconv.Atoi(partsA[i])
432+
numB, _ := strconv.Atoi(partsB[i])
433+
if c := cmp.Compare(numA, numB); c != 0 {
434+
return c
435+
}
431436
}
432437

433-
minorA, _ := strconv.Atoi(partsA[1])
434-
minorB, _ := strconv.Atoi(partsB[1])
435-
if c := cmp.Compare(minorA, minorB); c != 0 {
436-
return c
437-
}
438-
patchA, _ := strconv.Atoi(partsA[2])
439-
patchB, _ := strconv.Atoi(partsB[2])
440-
return cmp.Compare(patchA, patchB)
438+
// If all common parts are identical (e.g., "1.2" vs "1.2.3"),
439+
// the version with more parts is considered greater.
440+
return cmp.Compare(len(partsA), len(partsB))
441441
}

0 commit comments

Comments
 (0)