Skip to content

Commit cc11126

Browse files
Qelxirossylvestre
authored andcommitted
head: return to parse::<usize> and switch to parse_size_u64_max
1 parent 1ffaf2d commit cc11126

File tree

2 files changed

+19
-26
lines changed

2 files changed

+19
-26
lines changed

src/uu/head/src/parse.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// file that was distributed with this source code.
55

66
use std::ffi::OsString;
7-
use uucore::parser::parse_size::{ParseSizeError, parse_size_u64};
7+
use uucore::parser::parse_size::{ParseSizeError, parse_size_u64_max};
88

99
#[derive(PartialEq, Eq, Debug)]
1010
pub struct ParseError;
@@ -49,10 +49,11 @@ fn process_num_block(
4949
last_char: char,
5050
chars: &mut std::str::CharIndices,
5151
) -> Option<Result<Vec<OsString>, ParseError>> {
52-
let num = src.chars().fold(0u32, |acc, ch| {
53-
acc.saturating_mul(10)
54-
.saturating_add(ch.to_digit(10).unwrap())
55-
});
52+
let num = match src.parse::<usize>() {
53+
Ok(n) => n,
54+
Err(e) if *e.kind() == std::num::IntErrorKind::PosOverflow => usize::MAX,
55+
_ => return Some(Err(ParseError)),
56+
};
5657
let mut quiet = false;
5758
let mut verbose = false;
5859
let mut zero_terminated = false;
@@ -129,7 +130,7 @@ pub fn parse_num(src: &str) -> Result<(u64, bool), ParseSizeError> {
129130
if trimmed_string.is_empty() {
130131
Ok((0, all_but_last))
131132
} else {
132-
parse_size_u64(trimmed_string).map(|n| (n, all_but_last))
133+
parse_size_u64_max(trimmed_string).map(|n| (n, all_but_last))
133134
}
134135
}
135136

@@ -193,11 +194,11 @@ mod tests {
193194
fn test_parse_obsolete_overflow_x64() {
194195
assert_eq!(
195196
obsolete("-1000000000000000m"),
196-
obsolete_result(&["-c", "4294967295"])
197+
obsolete_result(&["-c", "18446744073709551615"])
197198
);
198199
assert_eq!(
199200
obsolete("-10000000000000000000000"),
200-
obsolete_result(&["-n", "4294967295"])
201+
obsolete_result(&["-n", "18446744073709551615"])
201202
);
202203
}
203204

tests/by-util/test_head.rs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -321,24 +321,20 @@ fn test_bad_utf8_lines() {
321321
fn test_head_invalid_num() {
322322
new_ucmd!()
323323
.args(&["-c", "1024R", "emptyfile.txt"])
324-
.fails()
325-
.stderr_is(
326-
"head: invalid number of bytes: '1024R': Value too large for defined data type\n",
327-
);
324+
.succeeds()
325+
.no_output();
328326
new_ucmd!()
329327
.args(&["-n", "1024R", "emptyfile.txt"])
330-
.fails()
331-
.stderr_is(
332-
"head: invalid number of lines: '1024R': Value too large for defined data type\n",
333-
);
328+
.succeeds()
329+
.no_output();
334330
new_ucmd!()
335331
.args(&["-c", "1Y", "emptyfile.txt"])
336-
.fails()
337-
.stderr_is("head: invalid number of bytes: '1Y': Value too large for defined data type\n");
332+
.succeeds()
333+
.no_output();
338334
new_ucmd!()
339335
.args(&["-n", "1Y", "emptyfile.txt"])
340-
.fails()
341-
.stderr_is("head: invalid number of lines: '1Y': Value too large for defined data type\n");
336+
.succeeds()
337+
.no_output();
342338
#[cfg(target_pointer_width = "32")]
343339
{
344340
let sizes = ["1000G", "10T"];
@@ -350,10 +346,7 @@ fn test_head_invalid_num() {
350346
{
351347
let sizes = ["-1000G", "-10T"];
352348
for size in &sizes {
353-
new_ucmd!()
354-
.args(&["-c", size])
355-
.fails()
356-
.stderr_is("head: out of range integral type conversion attempted: number of -bytes or -lines is too large\n");
349+
new_ucmd!().args(&["-c", size]).succeeds().no_output();
357350
}
358351
}
359352
new_ucmd!()
@@ -778,8 +771,7 @@ fn test_value_too_large() {
778771

779772
new_ucmd!()
780773
.args(&["-n", format!("{MAX}0").as_str(), "lorem_ipsum.txt"])
781-
.fails()
782-
.stderr_contains("Value too large for defined data type");
774+
.succeeds();
783775
}
784776

785777
#[test]

0 commit comments

Comments
 (0)