Skip to content

Commit

Permalink
Limit magnitudes of From<FixedDecimal> for PluralOperands to prevent …
Browse files Browse the repository at this point in the history
…overflow. (#293)
  • Loading branch information
sffc authored Oct 6, 2020
1 parent 554b3b5 commit b100859
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
7 changes: 4 additions & 3 deletions components/pluralrules/src/operands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,12 @@ impl_integer_type!(u8 u16 u32 u64 u128 usize);
impl_signed_integer_type!(i8 i16 i32 i64 i128 isize);

impl From<&FixedDecimal> for PluralOperands {
/// Converts a [icu_num_util::FixedDecimal] to [PluralOperands]
/// Converts a [icu_num_util::FixedDecimal] to [PluralOperands]. Retains at most 18
/// digits each from the integer and fraction parts.
fn from(dec: &FixedDecimal) -> Self {
let mag_range = dec.magnitude_range();
let mag_high = *mag_range.end();
let mag_low = *mag_range.start();
let mag_high = std::cmp::min(17, *mag_range.end());
let mag_low = std::cmp::max(-18, *mag_range.start());

let mut i: u64 = 0;
for magnitude in (0..=mag_high).rev() {
Expand Down
28 changes: 28 additions & 0 deletions components/pluralrules/tests/fixtures/operands.json
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,34 @@
"f": 0,
"t": 0
}
},
{
"input": {
"from": 9876543010,
"pow10": 10
},
"expected": {
"n": 765430100000000000,
"i": 765430100000000000,
"v": 0,
"w": 0,
"f": 0,
"t": 0
}
},
{
"input": {
"from": 9876543010,
"pow10": -20
},
"expected": {
"n": 0.000000000098765430,
"i": 0,
"v": 18,
"w": 17,
"f": 98765430,
"t": 9876543
}
}
]
}

0 comments on commit b100859

Please sign in to comment.