Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/uucore/src/lib/features/format/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,8 @@ fn resolve_asterisk_width(
Some(CanAsterisk::Asterisk(loc)) => {
let nb = args.next_i64(loc);
if nb < 0 {
Some((usize::try_from(-(nb as isize)).ok().unwrap_or(0), true))
// Unsigned arithmetic, so `i64::MIN` (magnitude 2^63) doesn't overflow.
Some((usize::try_from(nb.unsigned_abs()).ok().unwrap_or(0), true))
} else {
Some((usize::try_from(nb).ok().unwrap_or(0), false))
}
Expand Down Expand Up @@ -670,6 +671,26 @@ mod tests {
)
);
}

#[test]
fn asterisk_i64_min_width() {
// Regression test for https://github.com/uutils/coreutils/issues/13766
// |i64::MIN| = 2^63 overflows i64, so the magnitude of a negative
// `*` width must be computed in unsigned arithmetic.
let expected = usize::try_from(i64::MIN.unsigned_abs()).unwrap_or(0);
for arg in [
FormatArgument::SignedInt(i64::MIN),
FormatArgument::Unparsed(i64::MIN.to_string().into()),
] {
assert_eq!(
Some((expected, true)),
resolve_asterisk_width(
Some(CanAsterisk::Asterisk(ArgumentLocation::NextArgument)),
&mut FormatArguments::new(&[arg]),
)
);
}
}
}

mod resolve_asterisk_precision {
Expand Down
16 changes: 16 additions & 0 deletions tests/by-util/test_printf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1538,6 +1538,22 @@ fn test_extreme_field_width_overflow() {
.stderr_contains("printf: write error"); //could contains additional message like "formatting width too large" not in GNU, thats fine.
}

#[test]
fn test_asterisk_width_i64_min_no_panic() {
// Regression test for https://github.com/uutils/coreutils/issues/13766

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make the comment shorter

// An `i64::MIN` '*' width used to panic with "attempt to negate with overflow".
// It must not panic: on 64-bit it fails with a write error, on 32-bit the
// width is clamped to 0 and printf succeeds.
let result = new_ucmd!()
.args(&["|%*d|", &i64::MIN.to_string(), "1"])
.run();
assert!(
result.succeeded() || result.code() == 1,
"printf must not panic on an i64::MIN '*' width (got exit code {})",
result.code()
);
}

#[test]
fn test_q_string_control_chars_with_quotes() {
// Test %q with control characters and single quotes combined.
Expand Down
Loading