Skip to content

Fix vfprintf and derived functions badly handling +/` flag conflict #1269

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

Merged
merged 1 commit into from
Aug 30, 2024

Conversation

GabrielRavier
Copy link
Collaborator

(see commit message for details)

The C standard states, for conversion specifications containing the +
flag:
> The result of a signed conversion always begins with a plus or minus
> sign. (It begins with a sign only when a value with a negative sign
> is converted if this flag is not specified.)
- C Standard, 7.23.6.2.6. The fprintf function

...meaning that for all conversion but signed conversions, the flag
does nothing (note that flags which would invoke UB when used on an
unexpected conversion explicitly state so).

However, cosmopolitan currently interprets a + flag as equivalent to a
` flag, where the ` flag stands for quoting a character/string,
meaning a program such as:

 #include <stdio.h>

int main()
{
    printf("%+c\n", '=');
}

will, under cosmopolitan, print:

'='

instead of:

=

(i.e. the output is quoted as if %`c was used as the conversion
specification)

This patch fixes this by making it so the ` flags and + flags are not
internally considered identical, and adds a test for the change.
@github-actions github-actions bot added the libc label Aug 30, 2024
@jart jart merged commit 6baf6cd into jart:master Aug 30, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants