fix(robustness): replace risky production unwrap() with typed error handling - #3465
Closed
jewoos2921 wants to merge 1 commit into
Closed
fix(robustness): replace risky production unwrap() with typed error handling#3465jewoos2921 wants to merge 1 commit into
jewoos2921 wants to merge 1 commit into
Conversation
…andling Remove unwrap() calls from production paths where runtime panics could be triggered by external input or platform-specific conditions: - cas_publish.rs: pack_path.to_str().unwrap() → ok_or_else(CasError)? Non-UTF-8 temp paths (e.g. Windows usernames with non-ASCII chars) would panic the relay during git index-pack. - validation.rs: 9x try_into().unwrap() in PNG/WebP/MP4/GIF metadata validation → expect() with documented invariant. While logically safe after bounds checks, expect() makes the invariant explicit and produces a clear diagnostic if the assumption ever breaks. - agents.rs: events.next().unwrap() → expect() with invariant comment. Already guarded by an is_empty() check above; made the safety explicit. Note: side_effects.rs role_str.parse().unwrap() and actor_member.unwrap() were also addressed but main already refactored this code to remove them.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Removes
unwrap()calls from production code paths where runtime panics could be triggered by external input or platform-specific conditions, as part of a codebase audit following AGENTS.md's no-unwrap-in-production policy.Changes
🔴
cas_publish.rs— real panic risk fixedpack_path.to_str().unwrap()→ok_or_else(CasError)?index-pack. Now returns a typedCasErrorinstead.🟡
validation.rs— invariant documentation (9 sites)try_into().unwrap()in PNG/WebP/MP4/GIF metadata validation →expect()with documented invariantexpect()makes the safety invariant explicit and produces a clear diagnostic if the assumption ever breaks during future refactoring.🟡
agents.rs— invariant documentationevents.next().unwrap()→expect()with invariant commentis_empty()early-return above; made the safety explicit.What was NOT changed
The remaining ~18 production
unwrap()calls are all in categories where failure is genuinely impossible (e.g."*".parse().unwrap()for a static header value,Response::builder().body(Body::from(...)).unwrap()where the body type is infallible, SHA-256 digest slicing). Changing these would add noise without improving safety.Note:
side_effects.rshad two riskyunwrap()calls (role string parsing, actor member lookup) that were also targeted, butmainalready refactored that code to eliminate them.Verification
cargo check— compilescargo clippy -- -D warnings— no warningscargo fmt --check— formattedcargo test -p buzz-media --lib— 103/103 passcargo test -p buzz-cli --lib— pass