Skip to content

Commit 677400e

Browse files
authored
fix: improve version_compare logic (#3548)
Coverity was warning about unreachable code because I forgot to put in an if statement. Also, updated comment to better explain how version compare is terrible in the face of real life versions. Signed-off-by: Terri Oda <[email protected]>
1 parent 2846d41 commit 677400e

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

cve_bin_tool/version_compare.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,20 @@ def version_compare(v1: str, v2: str):
149149

150150
# They're both of type letter567 and we'll convert them to be letter.567 and
151151
# run them through the compare function again
152-
# Honestly it's hard to guess if .dev3 is going to be more or less than .rc4
153-
# unless you know the project, so hopefully people don't expect that kind of range
154-
# matching
155-
v1_newstring = re.sub("([a-zA-Z]+)([0-9]+)", r"\1.\2", v1_array[i])
156-
v2_newstring = re.sub("([a-zA-Z]+)([0-9]+)", r"\1.\2", v2_array[i])
157-
print(f"`{v1_newstring}` and `{v2_newstring}`")
158-
return version_compare(v1_newstring, v2_newstring)
152+
# We will be dictionary comparing so that 4.alpha4 < 4.beta1
153+
# but this also means .dev3 < .rc4 (because d is before r)
154+
# which may make less sense depending on the project.
155+
letter_number = re.compile("^[a-zA-Z]+[0-9]+$")
156+
if re.match(letter_number, v1_array[i]) and re.match(
157+
letter_number, v2_array[i]
158+
):
159+
v1_letter_number = re.sub(
160+
"([a-zA-Z]+)([0-9]+)", r"\1.\2", v1_array[i]
161+
)
162+
v2_letter_number = re.sub(
163+
"([a-zA-Z]+)([0-9]+)", r"\1.\2", v2_array[i]
164+
)
165+
return version_compare(v1_letter_number, v2_letter_number)
159166

160167
# And if all else fails, just compare the strings
161168
if v1_array[i] > v2_array[i]:

0 commit comments

Comments
 (0)