Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: non-alphanumeric characters as separators #3565

Merged
merged 2 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions cve_bin_tool/version_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,11 @@ def parse_version(version_string: str):
versionString = version_string.strip()
versionArray = []

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

# Note: This expression may need improvement if we need to handle unicode

# remove any trailing . then split
versionString = versionString.strip(".")
Expand Down
3 changes: 3 additions & 0 deletions test/test_version_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def test_lt(self):
assert Version("0.0.0.20190813141303.74dc4d7220e7") < Version(
"0.0.0.20200813141303"
)
assert Version("1.1.0l.1~deb9u2") < Version("2.0.0-1+deb9u1")
assert Version("1.1.0l.1~deb9u2") < Version("1.1.0m")

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

def test_error(self):
"""Make sure 'unknown' and blank strings raise appropriate errors"""
Expand Down
Loading