Fix issues 774, 782 and 789 (printf precision bugs) (#790)

The C standard states that, within the context of a printf-family
function, when specifying the precision of a conversion specification:

> A negative precision argument is taken as if the precision were
> omitted.
- Quoth the C Standard, 7.23.6.1. The fprintf function

Cosmopolitan instead treated negative precision arguments as
though they had a value of 0, which was non-conforming. This
change fixes that. Another issue we found relates to:

> For o conversion, it increases the precision, if and only if
> necessary, to force the first digit of the result to be a zero (if
> the value and precision are both 0, a single 0 is printed).
- Quoth the C standard, 7.23.6.1.6. The fprintf function

When printing numbers in their alternative form, with a precision and
with a conversion specifier of o (octal), Cosmopolitan wasn't following
the standard in two ways:

1. When printing a value with a precision that results in 0-padding,
   cosmopolitan would still add an extra 0 even though this should be
   done "if and only if necessary"
2. When printing a value of 0 with a precision of 0, nothing is
   printed, even though the standard specifically states that a single
   0 is printed in this case

This change fixes those issues too. Furthermore, regression tests have
been introduced to ensure Cosmopolitan continues to be conformant
going forward.

Fixes #774 
Fixes #782 
Fixes #789
This commit is contained in:
Gabriel Ravier 2023-03-29 10:11:48 +02:00 committed by GitHub
parent 2f4335e081
commit 7f925e6be9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 4 deletions

View file

@ -100,11 +100,36 @@ TEST(fmt, x) {
_gc(xasprintf("%#020hX", (short)0xABCDEF)));
}
TEST(fmt, o) {
EXPECT_STREQ("0000000000000037777777634", _gc(xasprintf("%#.25o", -100)));
EXPECT_STREQ("0001777777777777777777634", _gc(xasprintf("%#.25lo", -100L)));
EXPECT_STREQ("0001777777777777777777634", _gc(xasprintf("%#.25llo", -100LL)));
EXPECT_STREQ("0", _gc(xasprintf("%#.o", 0)));
}
TEST(fmt, b) {
EXPECT_STREQ("000010100 ", _gc(xasprintf("%-14.9b", 20)));
}
TEST(fmt, s) {
EXPECT_STREQ("123456", _gc(xasprintf("%4.*s", -5, "123456")));
EXPECT_STREQ("123456789", _gc(xasprintf("%4.*s", -5, "123456789")));
EXPECT_STREQ("12345678901234567890",
_gc(xasprintf("%4.*s", -5, "12345678901234567890")));
EXPECT_STREQ("123456789012345678901234567890",
_gc(xasprintf("%4.*s", -5, "123456789012345678901234567890")));
EXPECT_STREQ(
"1234567890123456789012345678901234567890",
_gc(xasprintf("%4.*s", -5, "1234567890123456789012345678901234567890")));
EXPECT_STREQ(
"12345678901234567890123456789012345678901234567890",
_gc(xasprintf("%4.*s", -5,
"12345678901234567890123456789012345678901234567890")));
EXPECT_STREQ(
"123456789012345678901234567890123456789012345678901234567890",
_gc(xasprintf(
"%4.*s", -5,
"123456789012345678901234567890123456789012345678901234567890")));
EXPECT_STREQ("Wide character output test",
_gc(xasprintf("%S", L"Wide character output test")));
}