Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions docs/Secure-Coding-Guide-for-Python/CWE-710/CWE-1095/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,29 @@ In-place modification of mutable types such as `list`, `dict`, or `set` that are

## Non-Compliant Code Example (List)

This `noncompliant01.py` example will successfully remove the Bob from `userlist` but this modifies the original list `userlist` and is not recommended.
This `noncompliant01.py` example will remove only one name that starts with `B` despite trying to remove them all without any exception raised:

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

```py
""" Non-compliant Code Example """
userlist = ['Alice', 'Bob', 'Charlie']
userlist = ['Alice', 'Bob', 'Bill', 'Charlie']
print(f'Unmodified list: {userlist}')

for user in userlist:
if user == 'Bob':
if user.startswith('B'):
userlist.remove(user)

print(f'Modified list: {userlist}')
```

Output from above noncompliant01.py:

```bash
Unmodified list: ['Alice', 'Bob', 'Bill', 'Charlie']
Modified list: ['Alice', 'Bill', 'Charlie']
```

## Non-Compliant Code Example (Dict)

This `noncompliant02.py` example attempts to delete a dictionary entry, which will result in a `RuntimeError: Dictionary changed size during iteration error` being thrown.
Expand Down Expand Up @@ -63,12 +70,12 @@ The `compliant01.py` solution demonstrates both strategies. The first example cr

```py
""" Compliant Code Example """
userlist = ['Alice', 'Bob', 'Charlie']
userlist = ['Alice', 'Bob', 'Bill', 'Charlie']
print(f'Unmodified list: {userlist}')

# Create a copy
for user in userlist.copy():
if user == 'Bob':
if user.startswith('B'):
userlist.remove(user)

print(f'Modified list: {userlist}')
Expand All @@ -80,7 +87,7 @@ print(f'Unmodified list: {userlist2}')
# Create new list
activeusers = []
for user in userlist2:
if user != 'Bob':
if user.startswith('B'):
activeusers.append(user)
print(f'New list: {activeusers}')
```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# SPDX-FileCopyrightText: OpenSSF project contributors
# SPDX-License-Identifier: MIT
""" Compliant Code Example """
userlist = ['Alice', 'Bob', 'Charlie']
userlist = ['Alice', 'Bob', 'Bill', 'Charlie']
print(f'Unmodified list: {userlist}')

# Create a copy
for user in userlist.copy():
if user == 'Bob':
if user.startswith('B'):
userlist.remove(user)

print(f'Modified list: {userlist}')
Expand All @@ -18,6 +18,6 @@
# Create new list
activeusers = []
for user in userlist2:
if user != 'Bob':
if not user.startswith('B'):
activeusers.append(user)
print(f'New list: {activeusers}')
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# SPDX-FileCopyrightText: OpenSSF project contributors
# SPDX-License-Identifier: MIT
""" Non-compliant Code Example """
userlist = ['Alice', 'Bob', 'Charlie']
userlist = ['Alice', 'Bob', 'Bill', 'Charlie']
print(f'Unmodified list: {userlist}')

for user in userlist:
if user == 'Bob':
if user.startswith('B'):
userlist.remove(user)

print(f'Modified list: {userlist}')