mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 03:27:44 +00:00
nl: handle line number overflow
This commit is contained in:
parent
15c27cd545
commit
d604f709ce
2 changed files with 22 additions and 1 deletions
|
@ -344,7 +344,10 @@ fn nl<T: Read>(reader: &mut BufReader<T>, settings: &Settings) -> UResult<()> {
|
||||||
line
|
line
|
||||||
);
|
);
|
||||||
// update line number for the potential next line
|
// update line number for the potential next line
|
||||||
line_no += settings.line_increment;
|
match line_no.checked_add(settings.line_increment) {
|
||||||
|
Some(new_line_no) => line_no = new_line_no,
|
||||||
|
None => return Err(USimpleError::new(1, "line number overflow")),
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
let spaces = " ".repeat(settings.number_width + 1);
|
let spaces = " ".repeat(settings.number_width + 1);
|
||||||
println!("{spaces}{line}");
|
println!("{spaces}{line}");
|
||||||
|
|
|
@ -464,3 +464,21 @@ fn test_invalid_regex_numbering() {
|
||||||
.stderr_contains("invalid regular expression");
|
.stderr_contains("invalid regular expression");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_line_number_overflow() {
|
||||||
|
new_ucmd!()
|
||||||
|
.arg(format!("--starting-line-number={}", i64::MAX))
|
||||||
|
.pipe_in("a\nb")
|
||||||
|
.fails()
|
||||||
|
.stdout_is(format!("{}\ta\n", i64::MAX))
|
||||||
|
.stderr_is("nl: line number overflow\n");
|
||||||
|
|
||||||
|
new_ucmd!()
|
||||||
|
.arg(format!("--starting-line-number={}", i64::MIN))
|
||||||
|
.arg("--line-increment=-1")
|
||||||
|
.pipe_in("a\nb")
|
||||||
|
.fails()
|
||||||
|
.stdout_is(format!("{}\ta\n", i64::MIN))
|
||||||
|
.stderr_is("nl: line number overflow\n");
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue