Merge remote-tracking branch 'agent/benma-agent/fee-percentage-rounding'
What changed, and why it matters
This commit fixes how the BitBox02 hardware wallet rounds a fee percentage shown on screen. The old code used a standard floating-point rounding function that could produce wrong results for values very close to a rounding boundary, and it also added unwanted floating-point code to the firmware binary. The new code does the rounding with integer arithmetic and clamps out-of-range or invalid inputs. The visible effect is a slightly different displayed fee percentage in edge cases; there is no direct evidence this could be used to steal funds or run unauthorized code.
Treat as a hardening/correctness improvement. Reviewers should verify that the displayed fee percentage is the only user-facing value affected by this rounding and that the new clamping behavior cannot hide legitimate negative or overflow values elsewhere. No urgent security patch is indicated by the diff alone.
Security signals we found
Floating-point rounding replaced with explicit integer rounding
Out-of-range/NaN/negative/infinite inputs now clamped to a safe default
New CI symbol check prevents floating-point remainder functions that bloat firmware binary
Behavioral test coverage added for boundary, large, negative, NaN and infinity cases
Evidence from the diff
format_percentage() in src/rust/bitbox02-rust/src/workflow/transaction.rs converts a percentage to a fixed one-decimal string. Previously it used FloatCore::round(p * 10.), which relies on f64 rounding and could mis-round near half-unit boundaries due to floating-point representation. The patch replaces it with explicit truncation and a 0.5 threshold, plus saturating addition and a cast that maps negative/NaN/infinite values to 0. A CI check is added to prevent future fmod/fmodf symbols from being introduced. The change is defensive and improves determinism and binary size; it does not by itself demonstrate a security vulnerability.
Changed components
src/rust/bitbox02-rust/src/workflow/transaction.rs.ci/check-unwanted-symbolsInspect captured patch +39 / −1
### .ci/check-unwanted-symbols
@@ -41,6 +41,13 @@ check_firmware() {
"Rust fmt float formatting like {:.1} adds significant binary bloat." \
"Use integer arithmetic and format the resulting integer parts explicitly."
+ check_symbols \
+ "$elf" \
+ "floating-point remainder" \
+ "(^|::|[[:space:]])fmodf?($|::|[.[:space:]])" \
+ "Floating-point remainder adds significant binary bloat." \
+ "Avoid floating-point % and round via integer conversion, as in format_percentage()."
+
check_symbols \
"$elf" \
"strftime" \
### src/rust/bitbox02-rust/src/workflow/transaction.rs
@@ -7,7 +7,13 @@ use crate::hal::ui::UserAbort;
use alloc::string::String;
fn format_percentage(p: f64) -> String {
- let int: u64 = num_traits::float::FloatCore::round(p * 10.) as _;
+ let scaled = p * 10.;
+ let truncated = scaled as u64;
+ let int = if scaled - truncated as f64 >= 0.5 {
+ truncated.saturating_add(1)
+ } else {
+ truncated
+ };
util::decimal::format_no_trim(int, 1)
}
@@ -86,5 +92,30 @@ mod tests {
assert_eq!(format_percentage(10.1), "10.1");
assert_eq!(format_percentage(10.14), "10.1");
assert_eq!(format_percentage(10.15), "10.2");
+ assert_eq!(format_percentage(10.15_f64.next_down()), "10.1");
+ assert_eq!(format_percentage(10.15_f64.next_up()), "10.2");
+ assert_eq!(format_percentage(99.95), "100.0");
+ }
+
+ #[test]
+ fn test_format_percentage_large_values() {
+ assert_eq!(
+ format_percentage(450_359_962_737_049.7),
+ "450359962737049.7"
+ );
+ assert_eq!(
+ format_percentage((u64::MAX as f64).next_down() / 10.),
+ "1844674407370954956.8"
+ );
+ for p in [u64::MAX as f64 / 10., f64::MAX, f64::INFINITY] {
+ assert_eq!(format_percentage(p), "1844674407370955161.5");
+ }
+ }
+
+ #[test]
+ fn test_format_percentage_negative_and_nan() {
+ for p in [-0.05, -10.15, f64::NEG_INFINITY, f64::NAN] {
+ assert_eq!(format_percentage(p), "0.0");
+ }
}
}Why this scored 21/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.