diff options
author | kf zheng <100595273+Kev1n8@users.noreply.github.com> | 2024-07-01 21:15:32 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-07-01 15:15:32 +0200 |
commit | e6b6b2761b62bfe282ef1f49da922bbb7086213d (patch) | |
tree | 8bbad20c5ec5879871bf1721ee4ab5e7ffd260cd /tests/by-util | |
parent | ff389491cca0afbe687113036b72e1f51f2e4ee5 (diff) |
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>
Diffstat (limited to 'tests/by-util')
-rw-r--r-- | tests/by-util/test_printf.rs | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/tests/by-util/test_printf.rs b/tests/by-util/test_printf.rs index 7bf1fcfe5..26f556e8e 100644 --- a/tests/by-util/test_printf.rs +++ b/tests/by-util/test_printf.rs @@ -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"); +} |