Skip to content

Commit 7dd93c0

Browse files
committed
add tests for invalid precision check
1 parent 975603d commit 7dd93c0

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,7 @@ impl Display for FormatError {
9292
"format '{}' has no % directive",
9393
String::from_utf8_lossy(s)
9494
),
95-
Self::InvalidPrecision(precision) => write!(
96-
f,
97-
"invalid precision: '{}'",
98-
precision
99-
),
95+
Self::InvalidPrecision(precision) => write!(f, "invalid precision: '{}'", precision),
10096
// TODO: Error message below needs some work
10197
Self::WrongSpecType => write!(f, "wrong % directive type was given"),
10298
Self::IoError(_) => write!(f, "io error"),

tests/by-util/test_printf.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,3 +774,21 @@ fn format_spec_zero_string_fails() {
774774
// It is invalid to have the format spec '%0s'
775775
new_ucmd!().args(&["%0s", "3"]).fails().code_is(1);
776776
}
777+
778+
#[test]
779+
fn invalid_precision_fails() {
780+
// It is invalid to have length of output string greater than i32::MAX
781+
new_ucmd!()
782+
.args(&["%.*d", "2147483648", "0"])
783+
.fails()
784+
.stderr_is("printf: invalid precision: '2147483648'\n");
785+
}
786+
787+
#[test]
788+
fn float_invalid_precision_fails() {
789+
// It is invalid to have length of output string greater than i32::MAX
790+
new_ucmd!()
791+
.args(&["%.*f", "9999999999", "0"])
792+
.fails()
793+
.stderr_is("printf: invalid precision: '9999999999'\n");
794+
}

0 commit comments

Comments
 (0)