You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/Secure-Coding-Guide-for-Python/CWE-691/CWE-783/README.md
+51-27Lines changed: 51 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -50,46 +50,67 @@ If a method changes an object’s state (has side effects) and is called multipl
50
50
51
51
## Non-Compliant Code Example
52
52
53
-
This `noncompliant01.py`code is expected to provide labels for numbers obfuscates the evaluation and logic.
53
+
`noncompliant01.py` is expected to provide labels for numbers, but it unnecessarily obfuscates the evaluation and logic.
54
54
55
55
_[noncompliant01.py](noncompliant01.py):_
56
56
57
57
```python
58
58
"""Non-Compliant Code Example"""
59
59
60
60
61
-
deflabel(number: int) -> str:
62
-
a =int(number <0) # negative flag
63
-
b = (number &1) ^1# even flag (1 for even, 0 for odd)
64
-
c =int(number <5) # small flag
65
-
66
-
key = (a <<2) | (b <<1) | c # pack flags into a single key
67
-
68
-
parts = ("big", "small", "even", "even", "neg", "neg", "neg", "neg")
61
+
deflabel(number: int) -> list[str]:
62
+
key =int(number <5) # (1) small
63
+
key |= ((number &1) ^1) <<1# (2) for even, 0 for odd
64
+
key |= (number <0) <<2# (4) negative
65
+
key |= (number >0) <<3# (8) positive
66
+
67
+
parts = (
68
+
"big", # 0
69
+
"small", # 1
70
+
"even small", # 2
71
+
"even small", # 3
72
+
"neg", # 4
73
+
"neg small", # 5
74
+
"neg even small", # 6
75
+
"neg even small", # 7
76
+
"big", # 8
77
+
"big even", # 9
78
+
"neg big", # 10
79
+
"neg big even", # 11
80
+
"big", # 12
81
+
"big even", # 13
82
+
"neg big", # 14
83
+
"neg big even", # 15
84
+
)
69
85
70
86
permuted =tuple(parts[(i *5) &7] for i inrange(8))
71
87
72
88
idx = (key *5) &7
73
-
return permuted[idx]
89
+
return permuted[idx].split("")
74
90
75
91
76
-
for number inrange(-3, 3):
92
+
for number inrange(-6, 6):
77
93
print(f"{number} = {label(number)}")
78
-
79
94
```
80
95
81
96
_Example output of `noncompliant01.py`:_
82
97
83
98
```bash
84
-
-3 = neg
85
-
-2 = neg
86
-
-1 = neg
87
-
0 = even
88
-
1 = small
89
-
2 = even
99
+
-6 = ['neg', 'even', 'small']
100
+
-5 = ['neg', 'small']
101
+
-4 = ['neg', 'even', 'small']
102
+
-3 = ['neg', 'small']
103
+
-2 = ['neg', 'even', 'small']
104
+
-1 = ['neg', 'small']
105
+
0 = ['even', 'small']
106
+
1 = ['small']
107
+
2 = ['even', 'small']
108
+
3 = ['small']
109
+
4 = ['even', 'small']
110
+
5 = ['big']
90
111
```
91
112
92
-
Attempting to add a label for `zero`will be challenging.
113
+
The `noncompliant01.py` does respond with the correct output. Extending the `noncompliant01.py`to also a label `postive` or `zero`numbers would be challenging.
<td>7.2.1. Augmented assignment statements [online]. Available from: <a href="https://docs.python.org/3/reference/simple_stmts.html?highlight=augmented%20assignment%20operators#augmented-assignment-statements">https://docs.python.org/3/reference/simple_stmts.html?highlight=augmented%20assignment%20operators#augmented-assignment-statement</a>, [Accessed 19 September 2025]</td>
194
+
<td>6. Expressions [online]. Available from: <a href="https://docs.python.org/3/reference/expressions.html#index-59">https://docs.python.org/3/reference/expressions.html#index-59</a>, [Accessed 19 September 2025]</td>
171
195
</tr>
172
196
<tr>
173
197
<td>[PLR 2022]</td>
174
-
<td>6.16. Evaluation order [online]. Available from: <a href="https://docs.python.org/3/reference/expressions.html#evaluation-orde">https://docs.python.org/3/reference/expressions.html#evaluation-orde</a>, [Accessed 19 September 2025]</td>
198
+
<td>6.16. Evaluation order [online]. Available from: <a href="https://docs.python.org/3/reference/expressions.html#evaluation-order">https://docs.python.org/3/reference/expressions.html#evaluation-order</a>, [Accessed 19 September 2025]</td>
0 commit comments