Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

printf: Check precision before writing into stdout. Fix #1879 #6511

Merged
merged 6 commits into from
Jul 1, 2024
Merged
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
2 changes: 2 additions & 0 deletions src/uucore/src/lib/features/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
TooManySpecs(Vec<u8>),
NeedAtLeastOneSpec(Vec<u8>),
WrongSpecType,
InvalidPrecision(String),

Check warning on line 65 in src/uucore/src/lib/features/format/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/uucore/src/lib/features/format/mod.rs#L65

Added line #L65 was not covered by tests
}

impl Error for FormatError {}
Expand Down Expand Up @@ -91,6 +92,7 @@
"format '{}' has no % directive",
String::from_utf8_lossy(s)
),
Self::InvalidPrecision(precision) => write!(f, "invalid precision: '{precision}'"),
// TODO: Error message below needs some work
Self::WrongSpecType => write!(f, "wrong % directive type was given"),
Self::IoError(_) => write!(f, "io error"),
Expand Down
12 changes: 12 additions & 0 deletions src/uucore/src/lib/features/format/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,10 @@
let precision = resolve_asterisk(*precision, &mut args)?.unwrap_or(0);
let i = args.get_i64();

if precision as u64 > i32::MAX as u64 {
return Err(FormatError::InvalidPrecision(precision.to_string()));
}

num_format::SignedInt {
width,
precision,
Expand All @@ -393,6 +397,10 @@
let precision = resolve_asterisk(*precision, &mut args)?.unwrap_or(0);
let i = args.get_u64();

if precision as u64 > i32::MAX as u64 {
return Err(FormatError::InvalidPrecision(precision.to_string()));

Check warning on line 401 in src/uucore/src/lib/features/format/spec.rs

View check run for this annotation

Codecov / codecov/patch

src/uucore/src/lib/features/format/spec.rs#L401

Added line #L401 was not covered by tests
}

num_format::UnsignedInt {
variant: *variant,
precision,
Expand All @@ -415,6 +423,10 @@
let precision = resolve_asterisk(*precision, &mut args)?.unwrap_or(6);
let f = args.get_f64();

if precision as u64 > i32::MAX as u64 {
return Err(FormatError::InvalidPrecision(precision.to_string()));
}

num_format::Float {
width,
precision,
Expand Down
18 changes: 18 additions & 0 deletions tests/by-util/test_printf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -774,3 +774,21 @@ fn format_spec_zero_string_fails() {
// It is invalid to have the format spec '%0s'
new_ucmd!().args(&["%0s", "3"]).fails().code_is(1);
}

#[test]
fn invalid_precision_fails() {
// It is invalid to have length of output string greater than i32::MAX
new_ucmd!()
.args(&["%.*d", "2147483648", "0"])
.fails()
.stderr_is("printf: invalid precision: '2147483648'\n");
}

#[test]
fn float_invalid_precision_fails() {
// It is invalid to have length of output string greater than i32::MAX
new_ucmd!()
.args(&["%.*f", "2147483648", "0"])
.fails()
.stderr_is("printf: invalid precision: '2147483648'\n");
}
Loading