Skip to content

Commit 5ea9b74

Browse files
hyperpolymathclaude
andcommitted
fix: eliminate production .unwrap() panic sites (6 → 0 bare unwraps)
Converts all bare `.unwrap()` calls in production code (outside test modules) to `.expect("static regex is valid")` for the seven OnceLock static-regex initialisers in analyzer.rs — matches the pattern already used for the four LazyLock initialisers at the top of the file. Fixes the one genuine panic risk: bridge/classify.rs line 100 called `.first().unwrap()` on `fixed_versions` inside a branch guarded by `semver_fix_available`, but that flag being true did not guarantee a non-empty vec. Changed to `.first().map(String::as_str).unwrap_or("unknown")`. All 6 surviving `.expect()` calls are structurally-unreachable invariants with descriptive messages (infallible regex init; accumulator consumed by seal()). 200/200 lib tests passing. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent db603c3 commit 5ea9b74

2 files changed

Lines changed: 8 additions & 8 deletions

File tree

src/assail/analyzer.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1638,7 +1638,7 @@ impl Analyzer {
16381638
stats.threading_constructs += content.matches("std::thread").count();
16391639

16401640
let unchecked_malloc =
1641-
RE_UNCHECKED_MALLOC.get_or_init(|| Regex::new(r"malloc\([^)]+\)\s*;").unwrap());
1641+
RE_UNCHECKED_MALLOC.get_or_init(|| Regex::new(r"malloc\([^)]+\)\s*;").expect("static regex is valid"));
16421642
if unchecked_malloc.is_match(content) {
16431643
weak_points.push(WeakPoint {
16441644
file: None,
@@ -2494,7 +2494,7 @@ impl Analyzer {
24942494

24952495
// Unsafe apply
24962496
let apply_re =
2497-
RE_ELIXIR_APPLY.get_or_init(|| Regex::new(r"apply\([^,]+,\s*[^,]+,").unwrap());
2497+
RE_ELIXIR_APPLY.get_or_init(|| Regex::new(r"apply\([^,]+,\s*[^,]+,").expect("static regex is valid"));
24982498
if apply_re.is_match(content) {
24992499
weak_points.push(WeakPoint {
25002500
file: None,
@@ -4156,7 +4156,7 @@ impl Analyzer {
41564156
file_path: &str,
41574157
) -> Result<()> {
41584158
// FFI calls (@ prefix)
4159-
let ffi_re = RE_PONY_FFI.get_or_init(|| Regex::new(r"@[a-zA-Z_]\w*\[").unwrap());
4159+
let ffi_re = RE_PONY_FFI.get_or_init(|| Regex::new(r"@[a-zA-Z_]\w*\[").expect("static regex is valid"));
41604160
let ffi_count = ffi_re.find_iter(content).count();
41614161
stats.unsafe_blocks += ffi_count;
41624162

@@ -4361,7 +4361,7 @@ impl Analyzer {
43614361
.collect::<Vec<_>>()
43624362
.join("\n");
43634363
let unquoted_var =
4364-
RE_SHELL_UNQUOTED_VAR.get_or_init(|| Regex::new(r#"\$[A-Za-z_]\w*"#).unwrap());
4364+
RE_SHELL_UNQUOTED_VAR.get_or_init(|| Regex::new(r#"\$[A-Za-z_]\w*"#).expect("static regex is valid"));
43654365
let dollar_vars = unquoted_var.find_iter(&stripped_content).count();
43664366
// Only flag if high number of unquoted vars
43674367
if dollar_vars > 20 {
@@ -4710,9 +4710,9 @@ impl Analyzer {
47104710
) -> Result<()> {
47114711
// HTTP (insecure) URLs - should be HTTPS
47124712
// Count http:// URLs that are NOT localhost/127.0.0.1 (those are fine)
4713-
let http_re = RE_HTTP_URL.get_or_init(|| Regex::new(r#"http://[a-zA-Z0-9]"#).unwrap());
4713+
let http_re = RE_HTTP_URL.get_or_init(|| Regex::new(r#"http://[a-zA-Z0-9]"#).expect("static regex is valid"));
47144714
let http_localhost_re = RE_HTTP_LOCALHOST.get_or_init(|| {
4715-
Regex::new(r#"http://(localhost|127\.0\.0\.1|0\.0\.0\.0|\[::1\])"#).unwrap()
4715+
Regex::new(r#"http://(localhost|127\.0\.0\.1|0\.0\.0\.0|\[::1\])"#).expect("static regex is valid")
47164716
});
47174717
let http_total = http_re.find_iter(content).count();
47184718
let http_local = http_localhost_re.find_iter(content).count();
@@ -4733,7 +4733,7 @@ impl Analyzer {
47334733
// Hardcoded secrets patterns
47344734
let secret_re = RE_HARDCODED_SECRET.get_or_init(|| Regex::new(
47354735
r#"(?i)(api[_-]?key|api[_-]?secret|password|passwd|secret[_-]?key|access[_-]?token|private[_-]?key)\s*[=:]\s*["'][^"']{8,}"#
4736-
).unwrap());
4736+
).expect("static regex is valid"));
47374737
if secret_re.is_match(content) {
47384738
weak_points.push(WeakPoint {
47394739
file: None,

src/bridge/classify.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ fn classify_reachable(
9797
)
9898
} else if vuln.semver_fix_available {
9999
// Semver-compatible fix — easiest mitigation
100-
let fix_version = vuln.fixed_versions.first().unwrap();
100+
let fix_version = vuln.fixed_versions.first().map(String::as_str).unwrap_or("unknown");
101101
(
102102
Classification::Mitigable,
103103
format!(

0 commit comments

Comments
 (0)