Skip to content

Commit c650b8e

Browse files
authored
Cosmetic changes to CWE-755 README.md
Signed-off-by: BartyBoi1128 <[email protected]>
1 parent 506e6da commit c650b8e

File tree

1 file changed

+7
-7
lines changed
  • docs/Secure-Coding-Guide-for-Python/CWE-703/CWE-755

1 file changed

+7
-7
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ The full list of OS exceptions can be found in the Python documentation [[Python
1717

1818
It is important to handle those exceptions when performing file I/O operations.
1919

20-
## Non-Compliant Code Example (os.remove()/os.unlink())
20+
## Non-Compliant Code Example (`os.remove()`/`os.unlink()`)
2121

22-
This code example demonstrates an attempt to read a non-existent file using the os module. The `read_file()` function opens a file and reads its content using `os.open()` and `os.read()`. If the file does not exist, an `OSError` or `FileNotFoundError` will be raised when trying to access the randomly generated file name.
22+
This code example demonstrates an attempt to read a non-existent file using the `os` module. The `read_file()` function opens a file and reads its content using `os.open()` and `os.read()`. If the file does not exist, an `OSError` or `FileNotFoundError` will be raised when trying to access the randomly generated file name.
2323

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

@@ -44,7 +44,7 @@ def read_file(file):
4444
read_file(f"{uuid.uuid4()}.txt")
4545
```
4646

47-
## Compliant Solution (try/except blocks)
47+
## Compliant Solution (`try/except` blocks)
4848

4949
The file opening and reading should be surrounded by the `try/except` block. This way, we can catch the generic `OSError` and handle the error differently depending on its cause (such as the file not existing or it being a directory instead).
5050

@@ -82,7 +82,7 @@ def read_file(file):
8282
read_file(f"{uuid.uuid4()}.txt")
8383
```
8484

85-
## Non-Compliant Code Example (pathlib.Path.unlink())
85+
## Non-Compliant Code Example (`pathlib.Path.unlink()`)
8686

8787
The `pathlib` module also provides functions for opening and reading files. The `Path.read_text()` method attempts to read the content of the file represented by the `Path` object. If the file does not exist, it will raise a `FileNotFoundError`. In this code example, this exception is expected when attempting to read a randomly generated non-existent file.
8888

@@ -109,7 +109,7 @@ def read_file(file):
109109
read_file(f"{uuid.uuid4()}.txt")
110110
```
111111

112-
The `pathlib.Path.unlink()` function has an optional parameter `missing_ok` that will suppress the `FileNotFoundError on file deletion`, if the parameter's value is `True` . However, without proper handling, using this parameter will cause the script to fail silently.
112+
The `pathlib.Path.unlink()` function has an optional parameter `missing_ok` that will suppress the `FileNotFoundError` on file deletion, if the parameter's value is `True` . However, without proper handling, using this parameter will cause the script to fail silently.
113113

114114
*[noncompliant03.py](noncompliant03.py):*
115115

@@ -133,9 +133,9 @@ def delete_temporary_file(file):
133133
delete_temporary_file(f"{uuid.uuid4()}.txt")
134134
```
135135

136-
## Compliant Solution (pathlib module)
136+
## Compliant Solution (`pathlib` module)
137137

138-
Since the `pathlib` module uses the same exceptions as the os module, error handling can be implemented in the same way.
138+
Since the `pathlib` module uses the same exceptions as the `os` module, error handling can be implemented in the same way.
139139

140140
*[compliant02.py](compliant02.py):*
141141

0 commit comments

Comments
 (0)