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

printf: Check precision before writing into stdout (#6511)

* Add a new error type InvalidPrecision

* check if the precision is valid before writing to stdout when it is signedInt, unsigned, or float

* add tests for invalid precision check

* add tests for invalid precision check

* fix possible cross-platform issue that code failing to pass on some tests

* uucore/format: inline var in format string

---------

Co-authored-by: Daniel Hofstetter <daniel.hofstetter@42dh.com>
This commit is contained in:
kf zheng 2024-07-01 21:15:32 +08:00 committed by GitHub
parent ff389491cc
commit e6b6b2761b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 32 additions and 0 deletions

View file

@ -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");
}