Skip to content

Commit

Permalink
Patch issue with incorrect parsing of leading zeros.
Browse files Browse the repository at this point in the history
This affects integers where they have a leading 0 followed by a
non-digit character when the format API is enabled and for non-partial
parsers. This should return an error. Tests have been introduced to
avoid regressions.

Closes #98
  • Loading branch information
Alexhuszagh committed Sep 19, 2024
1 parent bb1cbf1 commit 9eeedfe
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 4 deletions.
67 changes: 67 additions & 0 deletions lexical-parse-float/tests/issue_98_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#![cfg(all(feature = "power-of-two", feature = "format"))]

use std::assert_eq;

use lexical_parse_float::FromLexicalWithOptions;
use lexical_parse_float::NumberFormatBuilder;
use lexical_parse_float::Options;
use lexical_util::error::Error;

#[test]
fn issue_98_test() {
const DECIMAL_FORMAT: u128 = NumberFormatBuilder::new()
.required_digits(true)
.no_positive_mantissa_sign(false)
.no_special(true)
.no_integer_leading_zeros(true)
.no_float_leading_zeros(true)
.build();
let result = f64::from_lexical_with_options::<DECIMAL_FORMAT>(b"1.1.0", &Options::new());
assert!(result.is_err());
assert_eq!(result.unwrap_err(), Error::InvalidDigit(3));
assert_eq!(
f64::from_lexical_partial_with_options::<DECIMAL_FORMAT>(b"1.1.0", &Options::new()),
Ok((1.1f64, 3))
);
assert_eq!(
f64::from_lexical_with_options::<DECIMAL_FORMAT>(b"1.1", &Options::new()),
Ok(1.1f64)
);
assert_eq!(
f64::from_lexical_partial_with_options::<DECIMAL_FORMAT>(b"1.1", &Options::new()),
Ok((1.1f64, 3))
);

let result = f64::from_lexical_with_options::<DECIMAL_FORMAT>(b"0.1.0", &Options::new());
assert!(result.is_err());
assert_eq!(result.unwrap_err(), Error::InvalidDigit(3));
assert_eq!(
f64::from_lexical_partial_with_options::<DECIMAL_FORMAT>(b"0.1.0", &Options::new()),
Ok((0.1f64, 3))
);
assert_eq!(
f64::from_lexical_with_options::<DECIMAL_FORMAT>(b"0.1", &Options::new()),
Ok(0.1f64)
);
assert_eq!(
f64::from_lexical_partial_with_options::<DECIMAL_FORMAT>(b"0.1", &Options::new()),
Ok((0.1f64, 3))
);

let result = f64::from_lexical_with_options::<DECIMAL_FORMAT>(b"01.1.0", &Options::new());
assert!(result.is_err());
assert_eq!(result.unwrap_err(), Error::InvalidLeadingZeros(0));

let result = f64::from_lexical_with_options::<DECIMAL_FORMAT>(b"00.1", &Options::new());
assert!(result.is_err());
assert_eq!(result.unwrap_err(), Error::InvalidLeadingZeros(0));

assert_eq!(
f64::from_lexical_partial_with_options::<DECIMAL_FORMAT>(b"10.1", &Options::new()),
Ok((10.1, 4))
);
assert_eq!(
f64::from_lexical_partial_with_options::<DECIMAL_FORMAT>(b"11.1", &Options::new()),
Ok((11.1, 4))
);
}
12 changes: 8 additions & 4 deletions lexical-parse-integer/src/algorithm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ macro_rules! into_ok_partial {

/// Return an error for a complete parser upon an invalid digit.
macro_rules! invalid_digit_complete {
($value:ident, $index:expr) => {
($value:expr, $index:expr) => {
// Don't do any overflow checking here: we don't need it.
into_error!(InvalidDigit, $index - 1)
};
Expand All @@ -74,7 +74,7 @@ macro_rules! invalid_digit_complete {
/// Return a value for a partial parser upon an invalid digit.
/// This checks for numeric overflow, and returns the appropriate error.
macro_rules! invalid_digit_partial {
($value:ident, $index:expr) => {
($value:expr, $index:expr) => {
// NOTE: The value is already positive/negative
into_ok_partial!($value, $index - 1)
};
Expand Down Expand Up @@ -548,6 +548,7 @@ macro_rules! parse_digits_checked {
/// * `into_ok` - Behavior when returning a valid value.
/// * `invalid_digit` - Behavior when an invalid digit is found.
/// * `no_multi_digit` - If to disable multi-digit optimizations.
/// * `is_partial` - If the parser is a partial parser.
#[rustfmt::skip]
macro_rules! algorithm {
($bytes:ident, $into_ok:ident, $invalid_digit:ident, $no_multi_digit:expr) => {{
Expand Down Expand Up @@ -618,11 +619,14 @@ macro_rules! algorithm {
if zeros > 1 {
return into_error!(InvalidLeadingZeros, index);
}
// NOTE: Zeros has to be 0 here, so our index == 1 or 2 (depending on sign)
match iter.peek().map(|&c| char_to_digit_const(c, format.radix())) {
// Valid digit, we have an invalid value.
Some(Some(_)) => return into_error!(InvalidLeadingZeros, index),
// Either not a digit that follows, or nothing follows.
_ => return $into_ok!(<T>::ZERO, index),
// Have a non-digit character that follows.
Some(None) => return $invalid_digit!(<T>::ZERO, iter.cursor() + 1),
// No digits following, has to be ok
None => return $into_ok!(<T>::ZERO, index),
};
}
}
Expand Down
67 changes: 67 additions & 0 deletions lexical-parse-integer/tests/issue_98_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#![cfg(all(feature = "power-of-two", feature = "format"))]

use lexical_parse_integer::FromLexicalWithOptions;
use lexical_parse_integer::NumberFormatBuilder;
use lexical_parse_integer::Options;
use lexical_util::error::Error;

#[test]
fn issue_98_test() {
const DECIMAL_FORMAT: u128 = NumberFormatBuilder::new()
.required_digits(true)
.no_positive_mantissa_sign(false)
.no_special(true)
.no_integer_leading_zeros(true)
.no_float_leading_zeros(false)
.build();
let result = i64::from_lexical_with_options::<DECIMAL_FORMAT>(b"1.1.0", &Options::new());
assert!(result.is_err());
assert_eq!(result.unwrap_err(), Error::InvalidDigit(1));
assert_eq!(
i64::from_lexical_partial_with_options::<DECIMAL_FORMAT>(b"1.1.0", &Options::new()),
Ok((1, 1))
);

let result = i64::from_lexical_with_options::<DECIMAL_FORMAT>(b"1.1", &Options::new());
assert!(result.is_err());
assert_eq!(result.unwrap_err(), Error::InvalidDigit(1));
assert!(i64::from_lexical_with_options::<DECIMAL_FORMAT>(b"1.1", &Options::new()).is_err());
assert_eq!(
i64::from_lexical_partial_with_options::<DECIMAL_FORMAT>(b"1.1", &Options::new()),
Ok((1, 1))
);

let result = i64::from_lexical_with_options::<DECIMAL_FORMAT>(b"0.1.0", &Options::new());
assert!(result.is_err());
assert_eq!(result.unwrap_err(), Error::InvalidDigit(1));
assert_eq!(
i64::from_lexical_partial_with_options::<DECIMAL_FORMAT>(b"0.1.0", &Options::new()),
Ok((0, 1))
);

let result = i64::from_lexical_with_options::<DECIMAL_FORMAT>(b"0.1", &Options::new());
assert!(result.is_err());
assert_eq!(result.unwrap_err(), Error::InvalidDigit(1));
assert!(i64::from_lexical_with_options::<DECIMAL_FORMAT>(b"0.1", &Options::new()).is_err());
assert_eq!(
i64::from_lexical_partial_with_options::<DECIMAL_FORMAT>(b"0.1", &Options::new()),
Ok((0, 1))
);

let result = i64::from_lexical_with_options::<DECIMAL_FORMAT>(b"01.1", &Options::new());
assert!(result.is_err());
assert_eq!(result.unwrap_err(), Error::InvalidLeadingZeros(0));

let result = i64::from_lexical_with_options::<DECIMAL_FORMAT>(b"00.1", &Options::new());
assert!(result.is_err());
assert_eq!(result.unwrap_err(), Error::InvalidLeadingZeros(0));

assert_eq!(
i64::from_lexical_partial_with_options::<DECIMAL_FORMAT>(b"10.1", &Options::new()),
Ok((10, 2))
);
assert_eq!(
i64::from_lexical_partial_with_options::<DECIMAL_FORMAT>(b"11.1", &Options::new()),
Ok((11, 2))
);
}

0 comments on commit 9eeedfe

Please sign in to comment.