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: Default left-justify integer conversion to 1 width #3061

Merged

Conversation

hbina
Copy link
Contributor

@hbina hbina commented Feb 4, 2022

When using left-justify with integer conversion (like printf '%-o'),
default the minimum width to 1.

Closes: #3050

Signed-off-by: Hanif Ariffin hanif.ariffin.4326@gmail.com

@tertsdiepraam
Copy link
Member

The logic of the SubParser code is a bit hard to follow. Do you know if there is a possibility where min_width_tmp is not just empty but an invalid integer? Is that a case that we have to account for?

@sylvestre
Copy link
Sponsor Contributor

@hbina ping?

@hbina
Copy link
Contributor Author

hbina commented Feb 27, 2022

I don't fully understand the Output Conversion Syntax but I think what's happening is this:

The syntax for the conversion is something like this

% [ param-no $] flags width [ . precision ] type conversion

Given the input %-10o, the parser tokenize -10 as a token for the width.
It then converts the input to isize since it can be negative.
However, the previous author didn't consider the fact that %-o is also perfectly valid, and it just means that there is no minimum width required.
The default is 1 because AFAIK, for octals if there are no argument provided to it, it will default to 0.

I am not good enough at printf to try out different combination to break this so I might be totally wrong.

Do you know if there is a possibility where min_width_tmp is not just empty but an invalid integer? Is that a case that we have to account for?

GNU says that if the conversion syntax is ill-formed, the behavior is simply unpredictable so I don't think we have to care about invalid input.

If the syntax of a conversion specification is invalid, unpredictable things will happen, so don’t do this. If there aren’t enough function arguments provided to supply values for all the conversion specifications in the template string, or if the arguments are not of the correct types, the results are unpredictable. If you supply more arguments than conversion specifications, the extra argument values are simply ignored; this is sometimes useful.

Found at the bottom of the manual.

@hbina hbina closed this Feb 27, 2022
@hbina hbina reopened this Feb 27, 2022
@sylvestre sylvestre force-pushed the hbina-printf-default-left-justify-legth branch from a731baf to 43858ac Compare March 7, 2022 06:00
@sylvestre sylvestre force-pushed the hbina-printf-default-left-justify-legth branch from 43858ac to a3df611 Compare April 13, 2022 09:58
When using left-justify with integer conversion (like `printf '%-o'`),
default the minimum width to 1.

Closes: uutils#3050

Signed-off-by: Hanif Ariffin <hanif.ariffin.4326@gmail.com>
@sylvestre sylvestre force-pushed the hbina-printf-default-left-justify-legth branch from a3df611 to 2e60dce Compare May 1, 2022 18:00
@tertsdiepraam tertsdiepraam merged commit 0322d46 into uutils:main May 2, 2022
@hbina hbina deleted the hbina-printf-default-left-justify-legth branch May 3, 2022 10:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

printf need better error message
3 participants