Skip to content

Commit c986fb7

Browse files
authored
seq: add overflow checks when parsing exponents (#6858)
* seq: remove ignore flag from test_invalid_float_point_fail_properly(#6235) * seq: prevent overflow in parse_exponent_no_decimal * seq: add tests for invalid floating point arguments * seq: add overflow checks when parsing decimal with exponent * seq: add overflow checks
1 parent df1203a commit c986fb7

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

src/uu/seq/src/numberparse.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,20 @@ fn parse_exponent_no_decimal(s: &str, j: usize) -> Result<PreciseNumber, ParseNu
106106

107107
let num_integral_digits = if is_minus_zero_float(s, &x) {
108108
if exponent > 0 {
109-
2usize + exponent as usize
109+
(2usize)
110+
.checked_add(exponent as usize)
111+
.ok_or(ParseNumberError::Float)?
110112
} else {
111113
2usize
112114
}
113115
} else {
114-
let total = j as i64 + exponent;
116+
let total = (j as i64)
117+
.checked_add(exponent)
118+
.ok_or(ParseNumberError::Float)?;
115119
let result = if total < 1 {
116120
1
117121
} else {
118-
total.try_into().unwrap()
122+
total.try_into().map_err(|_| ParseNumberError::Float)?
119123
};
120124
if x.sign() == Sign::Minus {
121125
result + 1
@@ -207,7 +211,9 @@ fn parse_decimal_and_exponent(
207211
let integral_part: f64 = s[..j].parse().map_err(|_| ParseNumberError::Float)?;
208212
if integral_part.is_sign_negative() {
209213
if exponent > 0 {
210-
2usize + exponent as usize
214+
2usize
215+
.checked_add(exponent as usize)
216+
.ok_or(ParseNumberError::Float)?
211217
} else {
212218
2usize
213219
}
@@ -217,15 +223,20 @@ fn parse_decimal_and_exponent(
217223
};
218224
// Special case: if the string is "-.1e2", we need to treat it
219225
// as if it were "-0.1e2".
220-
let total = if s.starts_with("-.") {
221-
i as i64 + exponent + 1
222-
} else {
223-
i as i64 + exponent
226+
let total = {
227+
let total = (i as i64)
228+
.checked_add(exponent)
229+
.ok_or(ParseNumberError::Float)?;
230+
if s.starts_with("-.") {
231+
total.checked_add(1).ok_or(ParseNumberError::Float)?
232+
} else {
233+
total
234+
}
224235
};
225236
if total < minimum as i64 {
226237
minimum
227238
} else {
228-
total.try_into().unwrap()
239+
total.try_into().map_err(|_| ParseNumberError::Float)?
229240
}
230241
};
231242

tests/by-util/test_seq.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -777,12 +777,22 @@ fn test_undefined() {
777777
}
778778

779779
#[test]
780-
#[ignore = "Need issue #6235 to be fixed"]
781780
fn test_invalid_float_point_fail_properly() {
782781
new_ucmd!()
783782
.args(&["66000e000000000000000000000000000000000000000000000000000009223372036854775807"])
784783
.fails()
785-
.stdout_only(""); // might need to be updated
784+
.no_stdout()
785+
.usage_error("invalid floating point argument: '66000e000000000000000000000000000000000000000000000000000009223372036854775807'");
786+
new_ucmd!()
787+
.args(&["-1.1e9223372036854775807"])
788+
.fails()
789+
.no_stdout()
790+
.usage_error("invalid floating point argument: '-1.1e9223372036854775807'");
791+
new_ucmd!()
792+
.args(&["-.1e9223372036854775807"])
793+
.fails()
794+
.no_stdout()
795+
.usage_error("invalid floating point argument: '-.1e9223372036854775807'");
786796
}
787797

788798
#[test]

0 commit comments

Comments
 (0)