Skip to content

Commit 92de53c

Browse files
authored
Merge pull request #7239 from RenjiSann/printf-excess-args
printf: Show warning message in case of excess arguments
2 parents 64476fd + ae6cb8f commit 92de53c

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/uu/printf/src/printf.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::io::stdout;
77
use std::ops::ControlFlow;
88
use uucore::error::{UResult, UUsageError};
99
use uucore::format::{parse_spec_and_escape, FormatArgument, FormatItem};
10-
use uucore::{format_usage, help_about, help_section, help_usage};
10+
use uucore::{format_usage, help_about, help_section, help_usage, show_warning};
1111

1212
const VERSION: &str = "version";
1313
const HELP: &str = "help";
@@ -48,6 +48,13 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
4848
// Without format specs in the string, the iter would not consume any args,
4949
// leading to an infinite loop. Thus, we exit early.
5050
if !format_seen {
51+
if let Some(arg) = args.next() {
52+
use FormatArgument::*;
53+
let Unparsed(arg_str) = arg else {
54+
unreachable!("All args are transformed to Unparsed")
55+
};
56+
show_warning!("ignoring excess arguments, starting with '{arg_str}'");
57+
}
5158
return Ok(());
5259
}
5360

@@ -59,6 +66,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
5966
};
6067
}
6168
}
69+
6270
Ok(())
6371
}
6472

tests/by-util/test_printf.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,11 @@ fn char_as_byte() {
679679

680680
#[test]
681681
fn no_infinite_loop() {
682-
new_ucmd!().args(&["a", "b"]).succeeds().stdout_only("a");
682+
new_ucmd!()
683+
.args(&["a", "b"])
684+
.succeeds()
685+
.stdout_is("a")
686+
.stderr_contains("warning: ignoring excess arguments, starting with 'b'");
683687
}
684688

685689
#[test]

0 commit comments

Comments
 (0)