File tree Expand file tree Collapse file tree 2 files changed +20
-0
lines changed Expand file tree Collapse file tree 2 files changed +20
-0
lines changed Original file line number Diff line number Diff line change @@ -333,6 +333,7 @@ pub enum ParseError {
333
333
Overflow ,
334
334
Underflow ,
335
335
InexactRounding ,
336
+ Unparseable ,
336
337
}
337
338
338
339
impl fmt:: Display for ParseError {
@@ -344,6 +345,7 @@ impl fmt::Display for ParseError {
344
345
ParseError :: Overflow => write ! ( f, "overflow" ) ,
345
346
ParseError :: Underflow => write ! ( f, "underflow" ) ,
346
347
ParseError :: InexactRounding => write ! ( f, "inexact rounding" ) ,
348
+ ParseError :: Unparseable => write ! ( f, "unparseable" ) ,
347
349
}
348
350
}
349
351
}
@@ -469,6 +471,12 @@ impl std::str::FromStr for ParsedDecimal128 {
469
471
}
470
472
471
473
fn round_decimal_str ( s : & str , precision : usize ) -> Result < & str , ParseError > {
474
+ // TODO: In 1.80+ there's split_at_checked to make sure the split doesn't
475
+ // panic if the index doesn't falls at a codepoint boundary, until then
476
+ // we can check it with s.is_char_boundary(precision)
477
+ if !s. is_char_boundary ( precision) {
478
+ return Err ( ParseError :: Unparseable ) ;
479
+ }
472
480
let ( pre, post) = s. split_at ( precision) ;
473
481
// Any nonzero trimmed digits mean it would be an imprecise round.
474
482
if post. chars ( ) . any ( |c| c != '0' ) {
Original file line number Diff line number Diff line change @@ -10,10 +10,22 @@ use crate::{
10
10
Binary ,
11
11
Bson ,
12
12
DateTime ,
13
+ Decimal128 ,
13
14
Regex ,
14
15
Timestamp ,
15
16
} ;
16
17
18
+ #[ test]
19
+ fn test_decimal128_doesnt_panic_on_bad_codepoint_boundary ( ) {
20
+ use crate :: decimal128:: ParseError ;
21
+ use std:: str:: FromStr ;
22
+ // idx 34 (Coefficient::MAX_DIGITS) on this string isn't a valid codepoint boundary
23
+ assert ! ( matches!(
24
+ Decimal128 :: from_str( "111111111111111111111111111111111❤" ) ,
25
+ Err ( ParseError :: Unparseable )
26
+ ) )
27
+ }
28
+
17
29
#[ test]
18
30
fn string_from_document ( ) {
19
31
let rawdoc = rawdoc ! {
You can’t perform that action at this time.
0 commit comments