1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

printf : no infinite loop

This commit is contained in:
Sudhakar Verma 2024-01-12 16:08:47 +05:30
parent edb77b8d59
commit 2aa8a3502f
2 changed files with 14 additions and 1 deletions

View file

@ -11,7 +11,7 @@ use std::ops::ControlFlow;
use clap::{crate_version, Arg, ArgAction, Command}; use clap::{crate_version, Arg, ArgAction, Command};
use uucore::error::{UResult, UUsageError}; use uucore::error::{UResult, UUsageError};
use uucore::format::{parse_spec_and_escape, FormatArgument}; use uucore::format::{parse_spec_and_escape, FormatArgument, FormatItem};
use uucore::{format_usage, help_about, help_section, help_usage}; use uucore::{format_usage, help_about, help_section, help_usage};
const VERSION: &str = "version"; const VERSION: &str = "version";
@ -46,6 +46,14 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
}; };
} }
// See #5815 - We don't need to iter on args if no format string seen
let format_seen = parse_spec_and_escape(format_string.as_ref())
.into_iter()
.any(|r| matches!(r, Ok(FormatItem::Spec(_))));
if !format_seen {
return Ok(());
}
while args.peek().is_some() { while args.peek().is_some() {
for item in parse_spec_and_escape(format_string.as_ref()) { for item in parse_spec_and_escape(format_string.as_ref()) {
match item?.write(stdout(), &mut args)? { match item?.write(stdout(), &mut args)? {

View file

@ -649,3 +649,8 @@ fn partial_char() {
fn char_as_byte() { fn char_as_byte() {
new_ucmd!().args(&["%c", "🙃"]).succeeds().stdout_only("ð"); new_ucmd!().args(&["%c", "🙃"]).succeeds().stdout_only("ð");
} }
#[test]
fn no_infinite_loop() {
new_ucmd!().args(&["a", "b"]).succeeds().stdout_only("a");
}