Skip to content

Commit aaf3a4d

Browse files
committed
some cosmetics
Signed-off-by: Helge Wehder <[email protected]>
1 parent fbc4ef9 commit aaf3a4d

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
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+
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.
44

55
This behavior is compliant with IEEE 754[[2024 Wikipedia]](https://en.wikipedia.org/wiki/IEEE_754) a hardware induced compromise.
6-
The [example01.py](example01.py) code demonstrates various comparisons of float('NaN') all resulting in False
6+
The [example01.py](example01.py) code demonstrates various comparisons of `float('NaN')` all resulting in `False`.
77

88
```python
99
""" Code Example """
@@ -23,9 +23,7 @@ print(foo == float("NaN") or
2323

2424
## Non-Compliant Code Example
2525

26-
This noncompliant code example [[2024 docs.python.org]](https://docs.python.org/3/reference/expressions.html#value-comparisons) attempts a direct comparison with NaN in
27-
28-
_value == float("NaN").
26+
This noncompliant code example [[2024 docs.python.org]](https://docs.python.org/3/reference/expressions.html#value-comparisons) attempts a direct comparison with `NaN` in `_value == float("NaN")`.
2927

3028
*[noncompliant01.py](noncompliant01.py):*
3129

@@ -53,13 +51,13 @@ print(balance_is_positive("NaN"))
5351

5452
```
5553

56-
The balance_is_positive method returns True for all 3 cases instead of throwing an ValureError exception for balance_is_positive("NaN")
54+
The `balance_is_positive` method returns `True` for all 3 cases instead of throwing an `ValureError` exception for `balance_is_positive("NaN")`.
5755

5856
## Compliant Solution
5957

6058
The `compliant01.py` the method Decimal.quantize is used to gain control over known rounding errors in floating point values.
6159

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

6462
*[compliant01.py](compliant01.py):*
6563

@@ -88,9 +86,9 @@ print(balance_is_positive("NaN"))
8886

8987
```
9088

91-
Decimal throws a decimal.InvalidOperation for NaN values, the controlled rounding causes only "0.01" to return True.
89+
`Decimal` throws a `decimal.InvalidOperation` for `NaN` values, the controlled rounding causes only `"0.01"` to return `True`.
9290

93-
In `compliant02.py` we use the math.isnan to very if the value passed is a valid float value.
91+
In `compliant02.py` we use the math.isnan to very if the value passed is a valid `float` value.
9492

9593
*[compliant02.py](compliant02.py):*
9694

@@ -122,7 +120,7 @@ print(balance_is_positive("NaN"))
122120

123121
```
124122

125-
The balance_is_poitive method will raise an ValueError for NaN values.
123+
The `balance_is_poitive` method will raise an `ValueError` for `NaN` values.
126124

127125
## Automated Detection
128126

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ It is __not production code__ and requires code-style or python best practices t
9292

9393
|[CWE-703: Improper Check or Handling of Exceptional Conditions](https://cwe.mitre.org/data/definitions/703.html)|Prominent CVE|
9494
|:----------------------------------------------------------------|:----|
95-
|[CWE-230: Improper Handling of Missing Values](CWE-703/CWE-230/.)||
95+
|[CWE-230: Improper Handling of Missing Values](CWE-703/CWE-230/README.md)||
9696
|[CWE-252: Unchecked Return Value](CWE-703/CWE-252/README.md)||
9797
|[CWE-390: Detection of Error Condition without Action](CWE-703/CWE-390/README.md)||
9898
|[CWE-392: Missing Report of Error Condition](CWE-703/CWE-392/README.md)||

0 commit comments

Comments
 (0)