Skip to content

Commit b945bcb

Browse files
authored
prevent trailing dots in integers (#1267)
1 parent f636403 commit b945bcb

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/input/shared.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ fn clean_int_str(mut s: &str) -> Option<Cow<str>> {
120120
// we don't want to parse as f64 then call `float_as_int` as it can lose precision for large ints, therefore
121121
// we strip `.0+` manually instead
122122
if let Some(i) = s.find('.') {
123-
if s[i + 1..].chars().all(|c| c == '0') {
123+
let decimal_part = &s[i + 1..];
124+
if !decimal_part.is_empty() && decimal_part.chars().all(|c| c == '0') {
124125
s = &s[..i];
125126
}
126127
}

tests/validators/test_int.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,18 @@
2626
('42', 42),
2727
(42.0, 42),
2828
('42.0', 42),
29+
('42.00', 42),
2930
('042', 42),
3031
('4_2', 42),
3132
('4_2.0', 42),
3233
('04_2.0', 42),
34+
(' 04_2.0 ', 42),
35+
# because zeros are striped before underscores this is not allowed
36+
(' 0_42.0 ', Err('Input should be a valid integer, unable to parse string as an integer')),
3337
('000001', 1),
3438
('123456789.0', 123_456_789),
39+
('1.', Err('Input should be a valid integer, unable to parse string as an integer')),
40+
('42.', Err('Input should be a valid integer, unable to parse string as an integer')),
3541
('123456789123456.00001', Err('Input should be a valid integer, unable to parse string as an integer')),
3642
(int(1e10), int(1e10)),
3743
(i64_max, i64_max),

0 commit comments

Comments
 (0)