Skip to content

Commit 010a78c

Browse files
committed
Added updates from comments
1 parent 1f9baa7 commit 010a78c

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

docs/Secure-Coding-Guide-for-Python/CWE-703/CWE-230/README.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
# CWE-230: Improper Handling of Missing Values
22

3-
In python, some datasets use NaN (not-a-number) to represent the missing data. This can be problematic as the NaN values are unordered. The NaN value should be stripped before as they can cause surprising or undefined behaviours in the statistics functions that sort or count occurrences [[2024 doc.python.org]](https://docs.python.org/3/library/statistics.html) Any ordered comparison of a number to a not-a-number value are False. A counter-intuitive implication is that not-a-number values are not equal to themselves.
3+
The `NaN` value should be stripped before as they can cause surprising or undefined behaviours in the statistics functions that sort or count occurrences [[2024 doc.python.org]](https://docs.python.org/3/library/statistics.html).
4+
In python, some datasets use `NaN` (not-a-number) to represent the missing data. This can be problematic as the `NaN` values are unordered. Any ordered comparison of a number to a not-a-number value are `False`. A counter-intuitive implication is that `not-a-number` values are not equal to themselves.
45

56
This behavior is compliant with IEEE 754[[2024 Wikipedia]](https://en.wikipedia.org/wiki/IEEE_754) a hardware induced compromise.
67
The [example01.py](example01.py) code demonstrates various comparisons of float('NaN') all resulting in False
78
```python
9+
# SPDX-FileCopyrightText: OpenSSF project contributors
10+
# SPDX-License-Identifier: MIT
811
""" Code Example """
912

1013
foo = float('NaN')
@@ -28,6 +31,8 @@ _value == float("NaN").
2831
*[noncompliant01.py](noncompliant01.py):*
2932

3033
```python
34+
# SPDX-FileCopyrightText: OpenSSF project contributors
35+
# SPDX-License-Identifier: MIT
3136
""" Non-compliant Code Example """
3237

3338

@@ -55,9 +60,9 @@ The balance_is_positive method returns True for all 3 cases instead of throwing
5560

5661
## Compliant Solution
5762

58-
The `compliant01.py` the method Decimal.quantize is used to gain control over known rounding errors in floating point values.
63+
In the `compliant01.py` code example, the method `Decimal.quantize` is used to gain control over known rounding errors in floating point values.
5964

60-
The decision by the balance_is_positive method is to ROUND_DOWN instead of the default ROUND_HALF_EVEN.
65+
The decision by the `balance_is_positive` method is to `ROUND_DOWN` instead of the default `ROUND_HALF_EVEN`.
6166

6267
*[compliant01.py](compliant01.py):*
6368

@@ -87,7 +92,7 @@ print(balance_is_positive("NaN"))
8792

8893
Decimal throws a decimal.InvalidOperation for NaN values, the controlled rounding causes only "0.01" to return True.
8994

90-
In `compliant02.py` we use the math.isnan to very if the value passed is a valid float value.
95+
In `compliant02.py` we use the `math.isnan` to verify if the value passed is a valid `float` value.
9196

9297
*[compliant02.py](compliant02.py):*
9398

@@ -103,7 +108,7 @@ def balance_is_positive(value: str) -> bool:
103108
_value = float(value)
104109
if math.isnan(_value) or _value is None:
105110
raise ValueError("Expected a float")
106-
if _value <= 0:
111+
if _value < 0.01:
107112
return False
108113
else:
109114
return True
@@ -125,13 +130,13 @@ The balance_is_poitive method will raise an ValueError for NaN values.
125130
|Tool|Version|Checker|Description|
126131
|:----|:----|:----|:----|
127132
|Bandit|1.7.4 on Python 3.10.4|Not Available||
128-
|flake8|flake8-4.0.1 on python 3.10.4||FS002 '.format' used|
133+
|flake8|flake8-4.0.1 on python 3.10.4||Not Available|
129134

130135
## Related Guidelines
131136

132137
|||
133138
|:---|:---|
134-
|[SEI CERT Coding Standard for Java](https://wiki.sei.cmu.edu/confluence/display/java/SEI+CERT+Oracle+Coding+Standard+for+Java)|[IDS06-J. Exclude unsanitized user input from format strings](https://wiki.sei.cmu.edu/confluence/display/java/IDS06-J.+Exclude+unsanitized+user+input+from+format+strings)|
139+
|[SEI CERT Coding Standard for Java](https://wiki.sei.cmu.edu/confluence/display/java/SEI+CERT+Oracle+Coding+Standard+for+Java)|[NUM07-J. Do not attempt comparisons with NaN](https://wiki.sei.cmu.edu/confluence/display/java/NUM07-J.+Do+not+attempt+comparisons+with+NaN)|
135140
|[ISO/IEC TR 24772:2013](https://wiki.sei.cmu.edu/confluence/display/java/Rule+AA.+References#RuleAA.References-ISO/IECTR24772-2013)|Injection RST|
136141
|[MITRE CWE Pillar](http://cwe.mitre.org/)|[CWE-703: Improper Check or Handling of Exceptional Conditions (mitre.org)](https://cwe.mitre.org/data/definitions/703.html)|
137142
|[MITRE CWE Pillar](http://cwe.mitre.org/)|[CWE-230: Improper Handling of Missing Values](https://cwe.mitre.org/data/definitions/230.html)|

docs/Secure-Coding-Guide-for-Python/CWE-703/CWE-230/compliant02.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
""" Non-compliant Code Example """
44
import math
55

6-
76
def balance_is_positive(value: str) -> bool:
87
"""Returns True if there is still enough value for a transaction"""
98
_value = float(value)
109
if math.isnan(_value) or _value is None:
1110
raise ValueError("Expected a float")
12-
if _value <= 0:
11+
if _value < 0.01:
1312
return False
1413
else:
1514
return True

0 commit comments

Comments
 (0)