mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-29 00:32:29 +00:00
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:
parent
7f21547122
commit
e3d28de8a6
6 changed files with 88 additions and 14 deletions
|
@ -27,7 +27,7 @@ TEST(fscanf, test_readAfterFloat) {
|
|||
EXPECT_EQ(4, fscanf(f, "%f%x%f%x", &f1, &i1, &f2, &i2));
|
||||
EXPECT_TRUE(isinf(f1));
|
||||
EXPECT_EQ(0xDEAD, i1);
|
||||
EXPECT_EQ(-0.125e-2f, f2);
|
||||
EXPECT_FLOAT_EXACTLY_EQ(-0.125e-2f, f2);
|
||||
EXPECT_EQ(0xBEEF, i2);
|
||||
fclose(f);
|
||||
}
|
||||
|
|
|
@ -338,17 +338,17 @@ TEST(sscanf, flexdecimal_hex) {
|
|||
TEST(sscanf, floating_point_simple) {
|
||||
float x = 666.666f, y = x, z = y;
|
||||
EXPECT_EQ(3, sscanf("0.3715 .3715 3715", "%f %f %f", &x, &y, &z));
|
||||
EXPECT_EQ(0.3715f, x);
|
||||
EXPECT_EQ(0.3715f, y);
|
||||
EXPECT_EQ(3715.0f, z);
|
||||
EXPECT_FLOAT_EXACTLY_EQ(0.3715f, x);
|
||||
EXPECT_FLOAT_EXACTLY_EQ(0.3715f, y);
|
||||
EXPECT_FLOAT_EXACTLY_EQ(3715.0f, z);
|
||||
}
|
||||
|
||||
TEST(sscanf, floating_point_simple_double_precision) {
|
||||
double x = 666.666, y = x, z = y;
|
||||
EXPECT_EQ(3, sscanf("0.3715 .3715 3715", "%lf %lf %lf", &x, &y, &z));
|
||||
EXPECT_EQ(0.3715, x);
|
||||
EXPECT_EQ(0.3715, y);
|
||||
EXPECT_EQ(3715.0, z);
|
||||
EXPECT_DOUBLE_EXACTLY_EQ(0.3715, x);
|
||||
EXPECT_DOUBLE_EXACTLY_EQ(0.3715, y);
|
||||
EXPECT_DOUBLE_EXACTLY_EQ(3715.0, z);
|
||||
}
|
||||
|
||||
TEST(sscanf, floating_point_nan) {
|
||||
|
@ -426,12 +426,12 @@ TEST(sscanf, floating_point_documentation_examples) {
|
|||
2, sscanf("0X1.BC70A3D70A3D7P+6 1.18973e+4932zzz -0.0000000123junk junk",
|
||||
"%f %f %f %f %f", &f, &g, &h, &i, &j));
|
||||
|
||||
EXPECT_EQ(111.11f, a);
|
||||
EXPECT_EQ(-2.22f, b);
|
||||
EXPECT_FLOAT_EXACTLY_EQ(111.11f, a);
|
||||
EXPECT_FLOAT_EXACTLY_EQ(-2.22f, b);
|
||||
EXPECT_TRUE(isnan(c));
|
||||
EXPECT_TRUE(isnan(d));
|
||||
EXPECT_TRUE(isinf(e));
|
||||
EXPECT_EQ(0X1.BC70A3D70A3D7P+6f, f);
|
||||
EXPECT_FLOAT_EXACTLY_EQ(0X1.BC70A3D70A3D7P+6f, f);
|
||||
EXPECT_TRUE(isinf(g));
|
||||
}
|
||||
|
||||
|
@ -445,12 +445,12 @@ TEST(sscanf, floating_point_documentation_examples_double_precision) {
|
|||
2, sscanf("0X1.BC70A3D70A3D7P+6 1.18973e+4932zzz -0.0000000123junk junk",
|
||||
"%lf %lf %lf %lf %lf", &f, &g, &h, &i, &j));
|
||||
|
||||
EXPECT_EQ(111.11, a);
|
||||
EXPECT_EQ(-2.22, b);
|
||||
EXPECT_DOUBLE_EXACTLY_EQ(111.11, a);
|
||||
EXPECT_DOUBLE_EXACTLY_EQ(-2.22, b);
|
||||
EXPECT_TRUE(isnan(c));
|
||||
EXPECT_TRUE(isnan(d));
|
||||
EXPECT_TRUE(isinf(e));
|
||||
EXPECT_EQ(0X1.BC70A3D70A3D7P+6, f);
|
||||
EXPECT_DOUBLE_EXACTLY_EQ(0X1.BC70A3D70A3D7P+6, f);
|
||||
EXPECT_TRUE(isinf(g));
|
||||
}
|
||||
|
||||
|
@ -506,3 +506,9 @@ TEST(scanf, n) {
|
|||
ASSERT_EQ(1848, port);
|
||||
ASSERT_EQ(12, len);
|
||||
}
|
||||
|
||||
TEST(sscanf, floating_point_hexadecimal) {
|
||||
double a = 0;
|
||||
ASSERT_EQ(1, sscanf("0x1.5014c3472bc2c0000000p-123", "%lf", &a));
|
||||
ASSERT_DOUBLE_EXACTLY_EQ(0x1.5014c3472bc2c0000000p-123, a);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue