From b945bcb985dac95a3d9619aede954f78ad13a2bb Mon Sep 17 00:00:00 2001 From: Samuel Colvin Date: Mon, 8 Apr 2024 15:05:27 +0100 Subject: [PATCH] prevent trailing dots in integers (#1267) --- src/input/shared.rs | 3 ++- tests/validators/test_int.py | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/input/shared.rs b/src/input/shared.rs index f4251a11d..33e007a30 100644 --- a/src/input/shared.rs +++ b/src/input/shared.rs @@ -120,7 +120,8 @@ fn clean_int_str(mut s: &str) -> Option> { // we don't want to parse as f64 then call `float_as_int` as it can lose precision for large ints, therefore // we strip `.0+` manually instead if let Some(i) = s.find('.') { - if s[i + 1..].chars().all(|c| c == '0') { + let decimal_part = &s[i + 1..]; + if !decimal_part.is_empty() && decimal_part.chars().all(|c| c == '0') { s = &s[..i]; } } diff --git a/tests/validators/test_int.py b/tests/validators/test_int.py index 775c65cc3..640234754 100644 --- a/tests/validators/test_int.py +++ b/tests/validators/test_int.py @@ -26,12 +26,18 @@ ('42', 42), (42.0, 42), ('42.0', 42), + ('42.00', 42), ('042', 42), ('4_2', 42), ('4_2.0', 42), ('04_2.0', 42), + (' 04_2.0 ', 42), + # because zeros are striped before underscores this is not allowed + (' 0_42.0 ', Err('Input should be a valid integer, unable to parse string as an integer')), ('000001', 1), ('123456789.0', 123_456_789), + ('1.', Err('Input should be a valid integer, unable to parse string as an integer')), + ('42.', Err('Input should be a valid integer, unable to parse string as an integer')), ('123456789123456.00001', Err('Input should be a valid integer, unable to parse string as an integer')), (int(1e10), int(1e10)), (i64_max, i64_max),