Skip to content

Commit dded1b5

Browse files
authored
uucore: bound hex float exponent to avoid BigDecimal scale overflow (#14845)
* uucore: bound hex float exponent to avoid BigDecimal scale overflow A hexadecimal float whose binary exponent fits in an i64 but is very large (e.g. 0x1p-9223372036854775808) made BigDecimal::powi overflow its i64 scale, which panics in debug builds and silently wraps in release builds. Treat such exponents as overflow/underflow, like the decimal branch does for scales outside the i32 range: since 2**4 > 10, any |exponent| > 4 * i32::MAX gives a decimal exponent that cannot fit in an i32 either. Fixes #14835 * uucore: tighten hex float exponent bound to i32::MAX * log2(10) The previous bound of 4 * i32::MAX was loose (log2(10) is ~3.32), so exponents such as 0x1p8000000000 still went through powi. Use floor(i32::MAX * log2(10)) instead, which matches the i32 decimal exponent limit applied to decimal inputs, and test right at the limit. * Keep the hex exponent bound explanation in one place The comment where the bound is applied repeated the MAX_HEX_EXPONENT doc. Move the whole reasoning into the constant's doc and point to it from the call site. * Put the hex exponent limit value in backticks for doc_markdown --------- Co-authored-by: Amarnath10i <amarnath10i@users.noreply.github.com>
1 parent f085992 commit dded1b5

2 files changed

Lines changed: 69 additions & 5 deletions

File tree

‎src/uucore/src/lib/features/parser/num_parser.rs‎

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,13 @@ fn make_error(overflow: bool, negative: bool) -> ExtendedParserError<ExtendedBig
402402
}
403403
}
404404

405+
/// Largest binary exponent (in absolute value) accepted for hexadecimal floats:
406+
/// `floor(i32::MAX * log2(10))` (= `7_133_786_260`). Since `2**e == 10**(e * log10(2))`,
407+
/// a larger exponent has a decimal exponent that does not fit in an `i32` (the limit
408+
/// the decimal parser applies) and would overflow the `BigDecimal` scale in `powi`,
409+
/// so it is treated as overflow/underflow instead.
410+
const MAX_HEX_EXPONENT: u64 = (i32::MAX as f64 * std::f64::consts::LOG2_10) as u64;
411+
405412
/// Construct an [`ExtendedBigDecimal`] based on parsed data
406413
fn construct_extended_big_decimal(
407414
digits: BigUint,
@@ -453,16 +460,14 @@ fn construct_extended_big_decimal(
453460
let bd = BigDecimal::from_bigint(signed_digits, 0)
454461
/ BigDecimal::from_bigint(BigInt::from(16).pow(scale as u32), 0);
455462

456-
// powi "only" supports i64 values. Just overflow/underflow if the value provided
457-
// is > 2**64 or < 2**-64.
463+
// See MAX_HEX_EXPONENT for why larger exponents overflow/underflow.
458464
let exponent = exponent
459465
.to_i64()
466+
.filter(|e| e.unsigned_abs() <= MAX_HEX_EXPONENT)
460467
.ok_or_else(|| make_error(exponent.is_positive(), negative))?;
461468

462469
// Confusingly, exponent is in base 2 for hex floating point numbers.
463470
let base: BigDecimal = 2.into();
464-
// Note: We cannot overflow/underflow BigDecimal here, as we will not be able to reach the
465-
// maximum/minimum scale (i64 range).
466471
let pow2 = base.powi(exponent);
467472

468473
bd * pow2
@@ -591,7 +596,7 @@ mod tests {
591596

592597
use crate::extendedbigdecimal::ExtendedBigDecimal;
593598

594-
use super::{ExtendedParser, ExtendedParserError};
599+
use super::{ExtendedParser, ExtendedParserError, MAX_HEX_EXPONENT};
595600

596601
#[test]
597602
fn test_decimal_u64() {
@@ -1035,6 +1040,53 @@ mod tests {
10351040
ExtendedBigDecimal::MinusZero
10361041
))
10371042
));
1043+
// Exponents that fit in an i64 but are still far too large for BigDecimal.
1044+
assert!(matches!(
1045+
ExtendedBigDecimal::extended_parse(&format!("0x1p{}", i64::MAX)),
1046+
Err(ExtendedParserError::Overflow(ExtendedBigDecimal::Infinity))
1047+
));
1048+
assert!(matches!(
1049+
ExtendedBigDecimal::extended_parse(&format!("0x1p{}", i64::MIN)),
1050+
Err(ExtendedParserError::Underflow(ebd)) if ebd == ExtendedBigDecimal::zero()
1051+
));
1052+
assert!(matches!(
1053+
ExtendedBigDecimal::extended_parse(&format!("-0x1p{}", i64::MIN)),
1054+
Err(ExtendedParserError::Underflow(
1055+
ExtendedBigDecimal::MinusZero
1056+
))
1057+
));
1058+
1059+
// Right at the limit: the largest accepted binary exponent still parses...
1060+
assert_eq!(MAX_HEX_EXPONENT, 7_133_786_260);
1061+
assert!(matches!(
1062+
ExtendedBigDecimal::extended_parse(&format!("0x1p{MAX_HEX_EXPONENT}")),
1063+
Ok(ExtendedBigDecimal::BigDecimal(_))
1064+
));
1065+
assert!(matches!(
1066+
ExtendedBigDecimal::extended_parse(&format!("0x1p-{MAX_HEX_EXPONENT}")),
1067+
Ok(ExtendedBigDecimal::BigDecimal(_))
1068+
));
1069+
// ... and one past it overflows/underflows.
1070+
assert!(matches!(
1071+
ExtendedBigDecimal::extended_parse(&format!("0x1p{}", MAX_HEX_EXPONENT + 1)),
1072+
Err(ExtendedParserError::Overflow(ExtendedBigDecimal::Infinity))
1073+
));
1074+
assert!(matches!(
1075+
ExtendedBigDecimal::extended_parse(&format!("-0x1p{}", MAX_HEX_EXPONENT + 1)),
1076+
Err(ExtendedParserError::Overflow(
1077+
ExtendedBigDecimal::MinusInfinity
1078+
))
1079+
));
1080+
assert!(matches!(
1081+
ExtendedBigDecimal::extended_parse(&format!("0x1p-{}", MAX_HEX_EXPONENT + 1)),
1082+
Err(ExtendedParserError::Underflow(ebd)) if ebd == ExtendedBigDecimal::zero()
1083+
));
1084+
assert!(matches!(
1085+
ExtendedBigDecimal::extended_parse(&format!("-0x1p-{}", MAX_HEX_EXPONENT + 1)),
1086+
Err(ExtendedParserError::Underflow(
1087+
ExtendedBigDecimal::MinusZero
1088+
))
1089+
));
10381090

10391091
// Not actually hex numbers, but the prefixes look like it.
10401092
assert_eq!(

‎tests/by-util/test_printf.rs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,18 @@ fn test_extreme_exponent_does_not_overflow() {
499499
.fails_with_code(1)
500500
.stderr_contains("Numerical result out of range")
501501
.stdout_is(zero);
502+
503+
// Same for hexadecimal floats whose binary exponent fits in an i64.
504+
new_ucmd!()
505+
.args(&[spec, "0x1p9223372036854775807"])
506+
.fails_with_code(1)
507+
.stderr_contains("Numerical result out of range")
508+
.stdout_contains("inf");
509+
new_ucmd!()
510+
.args(&[spec, "0x1p-9223372036854775808"])
511+
.fails_with_code(1)
512+
.stderr_contains("Numerical result out of range")
513+
.stdout_is(zero);
502514
}
503515
}
504516

0 commit comments

Comments
 (0)