Fix UB in gdtoa hexadecimal float scanf and strtod (#1288)

When reading hexadecimal floats, cosmopolitan would previously sometimes
print a number of warnings relating to undefined behavior on left shift:

third_party/gdtoa/gethex.c:172: ubsan warning: signed left shift changed
sign bit or overflowed 12 'int' 28 'int' is undefined behavior

This is because gdtoa assumes left shifts are safe when overflow happens
even on signed integers - this is false: the C standard considers it UB.
This is easy to fix, by simply casting the shifted value to unsigned, as
doing so does not change the value or the semantics of the left shifting
(except for avoiding the undefined behavior, as the C standard specifies
that unsigned overflow yields wraparound, avoiding undefined behaviour).

This commit does this, and adds a testcase that previously triggered UB.
(this also adds test macros to test for exact float equality, instead of
the existing {EXPECT,ASSERT}_FLOAT_EQ macros which only tests inputs for
being "almost equal" (with a significant epsilon) whereas exact equality
makes more sense for certain things such as reading floats from strings,
and modifies other testcases for sscanf/fscanf of floats to utilize it).
This commit is contained in:
Gabriel Ravier 2024-09-15 02:11:04 +02:00 committed by GitHub
parent 7f21547122
commit e3d28de8a6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 88 additions and 14 deletions

View file

@ -195,6 +195,13 @@ void TearDownOnce(void);
#define ASSERT_LDBL_LT(VAL, GOT) \
assertLongDoubleLessThan(VAL, GOT, #VAL " < " #GOT, true)
#define ASSERT_FLOAT_EXACTLY_EQ(WANT, GOT) \
assertLongDoubleExactlyEquals(FILIFU WANT, GOT, #GOT, true)
#define ASSERT_DOUBLE_EXACTLY_EQ(WANT, GOT) \
assertLongDoubleExactlyEquals(FILIFU WANT, GOT, #GOT, true)
#define ASSERT_LDBL_EXACTLY_EQ(WANT, GOT) \
assertLongDoubleExactlyEquals(FILIFU WANT, GOT, #GOT, true)
/*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § testing library » assert or log
*/
@ -271,6 +278,13 @@ void TearDownOnce(void);
#define EXPECT_LGBL_LT(VAL, GOT) \
expectLongDoubleLessThan(VAL, GOT, #VAL " < " #GOT, false)
#define EXPECT_FLOAT_EXACTLY_EQ(WANT, GOT) \
assertLongDoubleExactlyEquals(FILIFU WANT, GOT, #GOT, false)
#define EXPECT_DOUBLE_EXACTLY_EQ(WANT, GOT) \
assertLongDoubleExactlyEquals(FILIFU WANT, GOT, #GOT, false)
#define EXPECT_LDBL_EXACTLY_EQ(WANT, GOT) \
assertLongDoubleExactlyEquals(FILIFU WANT, GOT, #GOT, false)
/*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § testing library » implementation details
*/
@ -404,6 +418,7 @@ void testlib_formatbinaryashex(const char *, const void *, size_t, char **,
void testlib_formatbinaryasglyphs(const char16_t *, const void *, size_t,
char **, char **);
bool testlib_almostequallongdouble(long double, long double);
bool testlib_exactlyequallongdouble(long double, long double);
void testlib_incrementfailed(void);
void testlib_clearxmmregisters(void);
@ -696,5 +711,20 @@ forceinline void assertLongDoubleEquals(FILIFU_ARGS long double want,
testlib_onfail2(isfatal);
}
forceinline void assertLongDoubleExactlyEquals(FILIFU_ARGS long double want,
long double got,
const char *gotcode,
bool isfatal) {
++g_testlib_ran;
if (testlib_exactlyequallongdouble(want, got))
return;
if (g_testlib_shoulddebugbreak)
DebugBreak();
testlib_showerror(file, line, func, "assertLongDoubleExactlyEquals", "",
gotcode, testlib_formatfloat(want),
testlib_formatfloat(got));
testlib_onfail2(isfatal);
}
COSMOPOLITAN_C_END_
#endif /* COSMOPOLITAN_LIBC_TESTLIB_H_ */