Skip to content
Open
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
8 changes: 4 additions & 4 deletions bin/idstack-manifest-merge
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ def load_payload(payload_arg):
if payload_arg == "-":
try:
text = sys.stdin.read()
except Exception as exc:
except OSError as exc:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since sys.stdin.read() can raise a UnicodeDecodeError if the input stream contains invalid UTF-8 bytes, catching only OSError will allow UnicodeDecodeError (which inherits from ValueError) to propagate uncaught. This would cause the script to crash with a traceback instead of exiting gracefully via die(). Consider catching both OSError and UnicodeDecodeError.

Suggested change
except OSError as exc:
except (OSError, UnicodeDecodeError) as exc:

die(6, f"failed to read payload from stdin: {exc}")
else:
if not os.path.isfile(payload_arg):
die(5, f"payload file not found: {payload_arg}")
try:
with open(payload_arg, "r", encoding="utf-8") as handle:
text = handle.read()
except Exception as exc:
except OSError as exc:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since handle.read() can raise a UnicodeDecodeError if the payload file contains invalid UTF-8 bytes, catching only OSError will allow UnicodeDecodeError (which inherits from ValueError) to propagate uncaught. This would cause the script to crash with a traceback instead of exiting gracefully via die(). Consider catching both OSError and UnicodeDecodeError.

Suggested change
except OSError as exc:
except (OSError, UnicodeDecodeError) as exc:

die(6, f"failed to read payload file {payload_arg!r}: {exc}")
try:
return json.loads(text)
Expand All @@ -109,7 +109,7 @@ def load_manifest(manifest_path):
try:
with open(manifest_path, "r", encoding="utf-8") as handle:
text = handle.read()
except Exception as exc:
except OSError as exc:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since handle.read() can raise a UnicodeDecodeError if the manifest file contains invalid UTF-8 bytes, catching only OSError will allow UnicodeDecodeError (which inherits from ValueError) to propagate uncaught. This would cause the script to crash with a traceback instead of exiting gracefully via die(). Consider catching both OSError and UnicodeDecodeError.

Suggested change
except OSError as exc:
except (OSError, UnicodeDecodeError) as exc:

die(6, f"failed to read manifest {manifest_path!r}: {exc}")
try:
data = json.loads(text)
Expand All @@ -130,7 +130,7 @@ def write_atomic(manifest_path, data):
json.dump(data, handle, indent=2)
handle.write("\n")
os.replace(tmp_path, manifest_path)
except Exception as exc:
except OSError as exc:
try:
os.unlink(tmp_path)
except OSError:
Expand Down