Skip to content

Commit dfde6a1

Browse files
authored
fix(builtins): clamp printf precision to prevent panic on large values (#1010)
## Summary - Clamp user-controlled precision to 10000 in `FormatSpec::parse` - Prevents thread panic when precision exceeds Rust's `format!` u16 limit (65535) ## What & Why `printf "%.65536f" 1.0` caused a thread panic in Rust's `format!` macro, dumping a full stack backtrace to stderr that leaks internal paths and dependency versions. Now precision is clamped to a safe maximum (10000). ## Tests Added - `test_large_precision_no_panic` — verifies no panic on precision > 65535 - `test_normal_precision_still_works` — verifies normal precision works correctly Closes #1003
1 parent 48dddc2 commit dfde6a1

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

crates/bashkit/src/builtins/printf.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ impl Builtin for Printf {
6262
}
6363

6464
/// Parsed format specification
65+
// Max precision to prevent panics in Rust's format! macro (u16::MAX limit)
66+
const MAX_PRECISION: usize = 10000;
67+
6568
struct FormatSpec {
6669
left_align: bool,
6770
zero_pad: bool,
@@ -137,7 +140,7 @@ impl FormatSpec {
137140
if prec_str.is_empty() {
138141
Some(0)
139142
} else {
140-
prec_str.parse().ok()
143+
prec_str.parse().ok().map(|p: usize| p.min(MAX_PRECISION))
141144
}
142145
} else {
143146
None
@@ -730,4 +733,22 @@ mod tests {
730733
"should handle CJK chars"
731734
);
732735
}
736+
737+
#[test]
738+
fn test_large_precision_no_panic() {
739+
// Must not panic on precision > 65535
740+
let args = vec!["1.0".to_string()];
741+
let mut idx = 0;
742+
let result = format_string("%.99999f", &args, &mut idx);
743+
// Should produce output without panicking — precision clamped
744+
assert!(!result.is_empty());
745+
}
746+
747+
#[test]
748+
fn test_normal_precision_still_works() {
749+
let args = vec!["3.14159".to_string()];
750+
let mut idx = 0;
751+
let result = format_string("%.2f", &args, &mut idx);
752+
assert_eq!(result, "3.14");
753+
}
733754
}

0 commit comments

Comments
 (0)