Fix printf-family functions on long double inf (#1273)

Cosmopolitan's printf-family functions currently very poorly handle
being passed a long double infinity.

For instance, a program such as:

```cpp
#include <stdio.h>

int main()
{
    printf("%f\n", 1.0 / 0.0);
    printf("%Lf\n", 1.0L / 0.0L);
    printf("%e\n", 1.0 / 0.0);
    printf("%Le\n", 1.0L / 0.0L);
    printf("%g\n", 1.0 / 0.0);
    printf("%Lg\n", 1.0L / 0.0L);
}
```

will currently output the following:

```
inf
0.000000[followed by 32763 more zeros]
inf
N.aN0000e-32769
inf
N.aNe-32769
```

when the correct expected output would be:

```
inf
inf
inf
inf
inf
inf
```

This patch fixes this, and adds tests for the behavior.
This commit is contained in:
Gabriel Ravier 2024-09-01 22:10:48 +02:00 committed by GitHub
parent cca0edd62b
commit 75e161b27b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 44 additions and 8 deletions

View file

@ -35,3 +35,39 @@ TEST(snprintf, testPlusFlagOnChar) {
ASSERT_EQ(i, 1);
ASSERT_STREQ(buf, "=");
}
TEST(snprintf, testInf) {
char buf[10] = {};
int i = snprintf(buf, sizeof(buf), "%f", 1.0 / 0.0);
ASSERT_EQ(i, 3);
ASSERT_STREQ(buf, "inf");
memset(buf, 0, 4);
i = snprintf(buf, sizeof(buf), "%Lf", 1.0L / 0.0L);
ASSERT_EQ(i, 3);
ASSERT_STREQ(buf, "inf");
memset(buf, 0, 4);
i = snprintf(buf, sizeof(buf), "%e", 1.0 / 0.0);
ASSERT_EQ(i, 3);
ASSERT_STREQ(buf, "inf");
memset(buf, 0, 4);
i = snprintf(buf, sizeof(buf), "%Le", 1.0L / 0.0L);
ASSERT_EQ(i, 3);
ASSERT_STREQ(buf, "inf");
memset(buf, 0, 4);
i = snprintf(buf, sizeof(buf), "%g", 1.0 / 0.0);
ASSERT_EQ(i, 3);
ASSERT_STREQ(buf, "inf");
memset(buf, 0, 4);
i = snprintf(buf, sizeof(buf), "%Lg", 1.0L / 0.0L);
ASSERT_EQ(i, 3);
ASSERT_STREQ(buf, "inf");
for (i = 4; i < 10; ++i)
ASSERT_EQ(buf[i], '\0');
}