Skip to content

Commit 05b0e0c

Browse files
committed
CWE-681- 01 - README and code examples added
Signed-off-by: ebakrra <[email protected]>
1 parent 92b8f37 commit 05b0e0c

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# CWE-681: Avoid an uncontrolled loss of precision when passing floating-point literals to a Decimal constructor
2+
3+
When working with decimal numbers in Python, using floating-point literals as input to the Decimal constructor can lead to unintended imprecision due to the limitations of IEEE 754 floating-point representation; therefore, to ensure accurate decimal representation, it is advisable to avoid using floating-point literals.
4+
5+
## Noncompliant Code Example
6+
7+
In the `noncompliant01.py` code example, a floating-point value is given to the Decimal constructor. The decimal 0.45 cannot be exactly represented by a floating-point literal, and hence the output of the decimal is imprecise. This is because 0.45 as a floating-point representation deals with the number in binary. In binary, many decimal fractions cannot be represented exactly. As a result, 0.45 is stored as an approximate binary fraction in a float, and when this approximation is converted to a Decimal, the inexactness is preserved, leading to a result like "0.450000000000000011102230246251565404236316680908203125".
8+
9+
```py
10+
11+
""" Non-compliant Code Example """
12+
from decimal import Decimal
13+
print(Decimal(0.45))
14+
```
15+
16+
## Compliant Solution
17+
18+
In the `compliant01.py` code example, the floating-point value is passed as a string, allowing the value to be directly converted to a Decimal object with the exact decimal value. This is because the string representation is interpreted exactly as it appears, maintaining all the specified digits.
19+
20+
```py
21+
22+
""" Compliant Code Example """
23+
from decimal import Decimal
24+
print(Decimal("0.45"))
25+
```
26+
27+
## Related Guidelines
28+
29+
|||
30+
|:---|:---|
31+
|[SEI CERT Oracle Coding Standard for Java](https://wiki.sei.cmu.edu/confluence/display/java/SEI+CERT+Oracle+Coding+Standard+for+Java?src=breadcrumbs)|[NUM10-J. Do not construct BigDecimal objects from floating-point literals](https://wiki.sei.cmu.edu/confluence/display/java/NUM10-J.+Do+not+construct+BigDecimal+objects+from+floating-point+literals)|
32+
|[MITRE CWE Base](http://cwe.mitre.org/)| [CWE-681](https://cwe.mitre.org/data/definitions/681.html)|
33+
|[MITRE CWE Pillar](http://cwe.mitre.org/)|[CWE-664: Improper Control of a Resource Through its Lifetime](https://cwe.mitre.org/data/definitions/664.html)|
34+
35+
## Automated Detection
36+
37+
|Tool|Version|Checker|Description|
38+
|:----|:----|:----|:----|
39+
|[Bandit](https://bandit.readthedocs.io/en/latest/)|1.7.4 on python 3.10.4|Not Available||
40+
|[Flake8](https://flake8.pycqa.org/en/latest/)|8-4.0.1 on python 3.10.4|Not Available||
41+
42+
## Biblography
43+
44+
|||
45+
|:---|:---|
46+
|[Python docs](https://docs.python.org/3/)|[Decimal](https://docs.python.org/3/library/decimal.html)|
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
""" Compliant Code Example """
2+
from decimal import Decimal
3+
print(Decimal("0.45"))
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
""" Non-compliant Code Example """
2+
from decimal import Decimal
3+
print(Decimal(0.45))

docs/Secure-Coding-Guide-for-Python/readme.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ It is __not production code__ and requires code-style or python best practices t
5454
|[CWE-532: Insertion of Sensitive Information into Log File](CWE-664/CWE-532/README.md)|[CVE-2023-45585](https://www.cvedetails.com/cve/CVE-2023-45585),<br/>CVSSv3.1: __9.8__,<br/>EPSS: __0.04__ (01.11.2024)|
5555
|[CWE-665: Improper Initialization](CWE-664/CWE-665/README.md)||
5656
|[CWE-681: Incorrect Conversion between Numeric Types](CWE-664/CWE-681/README.md)||
57+
|[CWE-681: Avoid an uncontrolled loss of precision when passing floating-point literals to a Decimal constructor.](CWE-664/CWE-681/01/README.md)||
5758
|[CWE-833: Deadlock](CWE-664/CWE-833/README.md)||
5859
|[CWE-843: Access of Resource Using Incompatible Type ('Type Confusion')](CWE-664/CWE-843/.)|[CVE-2021-29513](https://www.cvedetails.com/cve/CVE-2021-29513),<br/>CVSSv3.1: __7.8__,<br/>EPSS: __00.05__ (05.11.2024)|
5960
|[XXX-005: Consider hash-based integrity verification of byte code files against their source code files](CWE-664/XXX-005/.)||
@@ -71,7 +72,7 @@ It is __not production code__ and requires code-style or python best practices t
7172
|[CWE-693: Protection Mechanism Failure](https://cwe.mitre.org/data/definitions/693.html)|Prominent CVE|
7273
|:----------------------------------------------------------------|:----|
7374
|[CWE-184: Incomplete List of Disallowed Input](CWE-693/CWE-184/.)||
74-
|[CWE-330: Use of Insufficiently Random Values](CWE-693/CWE-330/README.md)|[CVE-2020-7548](https://www.cvedetails.com/cve/CVE-2020-7548),<br/>CVSSv3.1: **9.8**,<br/>EPSS: **0.22** (12.12.2024)|
75+
|[CWE-330: Use of Insufficiently Random Values](CWE-693/CWE-330/README.md)|[CVE-2020-7548](https://www.cvedetails.com/cve/CVE-2020-7548),<br/>CVSSv3.1: __9.8__,<br/>EPSS: __0.22__ (12.12.2024)|
7576
|[CWE-798: Use of hardcoded credentials](CWE-693/CWE-798/.)||
7677

7778
|[CWE-697: Incorrect Comparison](https://cwe.mitre.org/data/definitions/703.html)|Prominent CVE|
@@ -98,7 +99,7 @@ It is __not production code__ and requires code-style or python best practices t
9899
|:----------------------------------------------------------------|:----|
99100
|[CWE-1095: Loop Condition Value Update within the Loop](CWE-710/CWE-1095/README.md)||
100101
|[CWE-1109: Use of Same Variable for Multiple Purposes](CWE-710/CWE-1109/.)||
101-
|[CWE-489: Active Debug Code](CWE-710/CWE-489/README.md)|[CVE-2018-14649](https://www.cvedetails.com/cve/CVE-2018-14649),<br/>CVSSv3.1: **9.8**,<br/>EPSS: **69.64** (12.12.2023)|
102+
|[CWE-489: Active Debug Code](CWE-710/CWE-489/README.md)|[CVE-2018-14649](https://www.cvedetails.com/cve/CVE-2018-14649),<br/>CVSSv3.1: __9.8__,<br/>EPSS: __69.64__ (12.12.2023)|
102103

103104
## Biblography
104105

0 commit comments

Comments
 (0)