Skip to content

Commit

Permalink
Another rounding fix (#65)
Browse files Browse the repository at this point in the history
* Another rounding fix

* Match fix in Javascript implementation
  • Loading branch information
skontar authored Nov 1, 2024
1 parent 42a77b6 commit 1bfc39f
Show file tree
Hide file tree
Showing 7 changed files with 1,958 additions and 1,943 deletions.
2 changes: 1 addition & 1 deletion cvss/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
from .exceptions import CVSS2Error, CVSS3Error, CVSS4Error, CVSSError
from .interactive import ask_interactively

__version__ = "3.2"
__version__ = "3.3"
4 changes: 4 additions & 0 deletions cvss/constants4.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
# noinspection PyUnresolvedReferences
from ordereddict import OrderedDict

# Small enough value which can be added to a value which will be rounded to 1 decimal place so the
# result is rounded correctly despite floating point inaccuracies.
EPSILON = 10**-6

METRICS = [
"AV",
"AC",
Expand Down
17 changes: 14 additions & 3 deletions cvss/cvss4.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

from .constants4 import (
CVSS_LOOKUP_GLOBAL,
EPSILON,
MAX_COMPOSED,
MAX_SEVERITY,
METRICS,
Expand All @@ -52,8 +53,18 @@
from .exceptions import CVSS4MalformedError, CVSS4MandatoryError


def round_away_from_zero(x):
return float(D(x * 10).quantize(D("1"), rounding=ROUND_HALF_UP) / 10)
def final_rounding(x):
"""
Round to one decimal place. Use Decimal because Python float rounding defaults to
"round half to even". We actually want "round half away from zero" aka "round half up" for
positive numbers.
Add a small value to make sure that values like the following are correctly rounded despite
floating point inaccuracies:
8.6 - 7.15 = 1.4499999999999993 (float) => 1.5
"""
return float(D(x + EPSILON).quantize(D("0.1"), rounding=ROUND_HALF_UP))


class CVSS4(object):
Expand Down Expand Up @@ -547,7 +558,7 @@ def compute_base_score(self):
value = max(0.0, value)
value = min(10.0, value)

self.base_score = round_away_from_zero(value)
self.base_score = final_rounding(value)

def clean_vector(self, output_prefix=True):
"""
Expand Down
1,044 changes: 522 additions & 522 deletions tests/vectors_base4

Large diffs are not rendered by default.

2,088 changes: 1,044 additions & 1,044 deletions tests/vectors_modified4

Large diffs are not rendered by default.

734 changes: 367 additions & 367 deletions tests/vectors_random4

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions tests/vectors_threat4
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:N/VI:N/VA:N/SC:N/SI:N/SA:N/E:A - 0.0
CVSS:4.0/AV:P/AC:H/AT:P/PR:H/UI:A/VC:H/VI:H/VA:H/SC:H/SI:H/SA:H/E:A - 7.3
CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:N/VI:N/VA:N/SC:N/SI:N/SA:N/E:P - 0.0
CVSS:4.0/AV:P/AC:H/AT:P/PR:H/UI:A/VC:H/VI:H/VA:H/SC:H/SI:H/SA:H/E:P - 5.9
CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:N/VI:N/VA:N/SC:N/SI:N/SA:N/E:U - 0.0
CVSS:4.0/AV:P/AC:H/AT:P/PR:H/UI:A/VC:H/VI:H/VA:H/SC:H/SI:H/SA:H/E:U - 4.1
CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:N/VI:N/VA:N/SC:N/SI:N/SA:N/E:A - 0.0
CVSS:4.0/AV:P/AC:H/AT:P/PR:H/UI:A/VC:H/VI:H/VA:H/SC:H/SI:H/SA:H/E:A - 7.3
CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:N/VI:N/VA:N/SC:N/SI:N/SA:N/E:P - 0.0
CVSS:4.0/AV:P/AC:H/AT:P/PR:H/UI:A/VC:H/VI:H/VA:H/SC:H/SI:H/SA:H/E:P - 5.9
CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:N/VI:N/VA:N/SC:N/SI:N/SA:N/E:U - 0.0
CVSS:4.0/AV:P/AC:H/AT:P/PR:H/UI:A/VC:H/VI:H/VA:H/SC:H/SI:H/SA:H/E:U - 4.1

0 comments on commit 1bfc39f

Please sign in to comment.