Skip to content

Commit 9923fe1

Browse files
authored
fix: non-alphanumeric characters as separators (#3565)
This switches the logic so we treat all non-alphanumeric characters as separators equivalent to `.` in version strings. This should make the code more robust to unusual version strings. * Fixes #3558 (in that we will be able to handle `~`) Signed-off-by: Terri Oda <[email protected]>
1 parent 677400e commit 9923fe1

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

cve_bin_tool/version_compare.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,11 @@ def parse_version(version_string: str):
4040
versionString = version_string.strip()
4141
versionArray = []
4242

43-
# convert - and _ to be treated like . below
43+
# convert all non alpha-numeric characters to be treated like . below
4444
# we could switch to a re split but it seems to leave blanks so this is less hassle
45-
versionString = versionString.replace("-", ".")
46-
versionString = versionString.replace("_", ".")
47-
versionString = versionString.replace("+", ".")
48-
# Note: there may be other non-alphanumeric characters we want to add here in the
49-
# future, but we'd like to look at those cases before adding them in case the version
50-
# logic is very different.
45+
versionString = re.sub("[^0-9a-zA-Z]+", ".", versionString)
46+
47+
# Note: This expression may need improvement if we need to handle unicode
5148

5249
# remove any trailing . then split
5350
versionString = versionString.strip(".")

test/test_version_compare.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ def test_lt(self):
3434
assert Version("0.0.0.20190813141303.74dc4d7220e7") < Version(
3535
"0.0.0.20200813141303"
3636
)
37+
assert Version("1.1.0l.1~deb9u2") < Version("2.0.0-1+deb9u1")
38+
assert Version("1.1.0l.1~deb9u2") < Version("1.1.0m")
3739

3840
def test_gt(self):
3941
"""Make sure > works between versions, including some with unusual version schemes"""
@@ -50,6 +52,7 @@ def test_gt(self):
5052
assert Version("0.0.0.20200813141303") > Version(
5153
"0.0.0.20190813141303.74dc4d7220e7"
5254
)
55+
assert Version("1.1.0m") > Version("1.1.0l.1~deb9u2")
5356

5457
def test_error(self):
5558
"""Make sure 'unknown' and blank strings raise appropriate errors"""

0 commit comments

Comments
 (0)