Skip to content

Commit fc85cc8

Browse files
authored
fix: treat 1.0 and 1 as the same for excel users (#4543)
* fix: treat 1.0 and 1 as the same for excel users * fixes #4467 If you edit a csv/spreadsheet in excel, it will modify values that "look like" integers to it, so the version 1.0 becomes 1, truncating the final ".0" from the version string. This adds an edge case in to the version compare function so it treats these truncated versions as the same (which was the behaviour in previous versions of cve-bin-tool). Signed-off-by: Terri Oda <[email protected]>
1 parent 7153e9e commit fc85cc8

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

cve_bin_tool/version_compare.py

+18
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@ def version_compare(v1: str, v2: str):
134134
if v1_array[i] in pre_release_words:
135135
return -1
136136

137+
# special edge case for folk editing version info in excel
138+
# who may lose the trailing .0 in versions like 1.0
139+
try:
140+
if int(v1_array[i]) == 0 and len(v1_array) == len(v2_array) + 1:
141+
return 0
142+
143+
except ValueError:
144+
return 1
145+
137146
# Otherwise, v1 has more digits than v2 and the previous ones matched,
138147
# so it's probably later. e.g. 1.2.3 amd 1.2.q are both > 1.2
139148
return 1
@@ -150,6 +159,15 @@ def version_compare(v1: str, v2: str):
150159
if v2_array[len(v1_array)] in pre_release_words:
151160
return 1
152161

162+
# special edge case for folk editing version info in excel
163+
# who may lose the trailing .0 in versions like 1.0
164+
try:
165+
if int(v2_array[len(v1_array)]) == 0 and len(v2_array) == len(v1_array) + 1:
166+
return 0
167+
168+
except ValueError:
169+
return -1
170+
153171
return -1
154172

155173
return 0

test/test_version_compare.py

+9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ def test_eq(self):
1616
assert Version("4.4.A") == Version("4.4.a")
1717
assert Version("5.6 ") == Version("5.6")
1818
assert Version("f835f2caaa") == Version("f835f2caaa")
19+
assert Version("42.0") == Version(
20+
"42"
21+
) # edge case for folk editing versions in excel
22+
assert Version("1") == Version(
23+
"1.0"
24+
) # edge case for folk editing versions in excel
1925

2026
def test_lt(self):
2127
"""Make sure < works between versions, including some with unusual version schemes"""
@@ -75,3 +81,6 @@ def test_ne(self):
7581
"""Test some != cases with hashes to make sure we aren't comparing the string 'HASH'"""
7682
assert Version("f835f2caab") != Version("f835f2caaa")
7783
assert Version("HASH") != Version("f835f2caaa")
84+
assert Version("1") != Version(
85+
"1.0.0"
86+
) # the edge case for excel only works on single .0

0 commit comments

Comments
 (0)