From cd4568f5d9862ab28dda351c7692c0bd95dcdda7 Mon Sep 17 00:00:00 2001 From: Sudhakar Verma Date: Fri, 12 Jan 2024 17:12:30 +0530 Subject: [PATCH] printf: simplify loop --- src/uu/printf/src/printf.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/uu/printf/src/printf.rs b/src/uu/printf/src/printf.rs index b0988f7b4..c98bb59a1 100644 --- a/src/uu/printf/src/printf.rs +++ b/src/uu/printf/src/printf.rs @@ -38,17 +38,20 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { None => vec![], }; + let mut format_seen = false; let mut args = values.iter().peekable(); for item in parse_spec_and_escape(format_string.as_ref()) { + if let Ok(FormatItem::Spec(_)) = item { + format_seen = true; + } match item?.write(stdout(), &mut args)? { ControlFlow::Continue(()) => {} ControlFlow::Break(()) => return Ok(()), }; } - // 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()).any(|r| matches!(r, Ok(FormatItem::Spec(_)))); + // Without format specs in the string, the iter would not consume any args, + // leading to an infinite loop. Thus, we exit early. if !format_seen { return Ok(()); }