diff --git a/src/hb-number-parser.hh b/src/hb-number-parser.hh index 7bcb3122a..53e1af829 100644 --- a/src/hb-number-parser.hh +++ b/src/hb-number-parser.hh @@ -28,9 +28,12 @@ #include "hb.hh" -static const double _powers_of_10[] = -{ 1.0e+256, 1.0e+128, 1.0e+64, 1.0e+32, 1.0e+16, 1.0e+8, 10000., 100., 10. }; - +static const double _small_powers_of_10[] = +{ 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15 }; +static const double _large_powers_of_10[] = +{ 1e0, 1e16, 1e32, 1e48, 1e64, 1e80, 1e96, 1e112, 1e128, 1e144, + 1e160, 1e176, 1e192, 1e208, 1e224, 1e240, 1e256, 1e272, 1e288, 1e304 }; + /* Grammar: * number := sign? mantissa exponent? * sign := '+' | '-' @@ -109,7 +112,7 @@ struct hb_number_parser_t return (state == Sign || state == AfterDotFromStart) ? 0 : pos; } - /* Valid after finish() returned true. */ + /* Valid after `commit ()` returned nonzero. */ double get_value () const { double v = value; @@ -213,15 +216,22 @@ private: } /* Handles 0..308; anything larger overflows double and returns HUGE_VAL. */ - static double _pow10 (unsigned exponent) + static double _pow10 (unsigned n) { /* DBL_MAX is between 10^308 and 10^309 */ - if (unlikely (exponent > 308)) return HUGE_VAL; - unsigned mask = 1 << (ARRAY_LENGTH (_powers_of_10) - 1); - double result = 1; - for (const double *power = _powers_of_10; mask; ++power, mask >>= 1) - if (exponent & mask) result *= *power; - return result; + constexpr unsigned MAX_POW10_EXP = 308; + constexpr unsigned POW10_SHIFT = 4; + constexpr unsigned POW10_SMALL = 1u << POW10_SHIFT; /* 16 */ + constexpr unsigned POW10_MASK = POW10_SMALL - 1; /* 15 */ + + static_assert (ARRAY_LENGTH (_small_powers_of_10) == POW10_SMALL, + "_small_powers_of_10: size must be 2^POW10_SHIFT"); + static_assert ((MAX_POW10_EXP >> POW10_SHIFT) < ARRAY_LENGTH (_large_powers_of_10), + "_large_powers_of_10: must cover MAX_POW10_EXP >> POW10_SHIFT"); + + if (unlikely (n > MAX_POW10_EXP)) return HUGE_VAL; + if (likely (n < POW10_SMALL)) return _small_powers_of_10[n]; + return _large_powers_of_10[n >> POW10_SHIFT] * _small_powers_of_10[n & POW10_MASK]; } }; diff --git a/src/test-number.cc b/src/test-number.cc index 33a313522..c3e7b4c85 100644 --- a/src/test-number.cc +++ b/src/test-number.cc @@ -1959,6 +1959,79 @@ test_parse_double (void) hb_always_assert (almost_equal (pv, strtod (str, nullptr))); hb_always_assert (pp == str + 19); // trailing 'e' not consumed } + /* n = 251 = 15*16 + 11: _large[15] * _small[11]. + * Fuzzer finding: '.1E251' was 3 ULP off with the composition table. */ + { + const char str[] = ".1E251"; + const char *pp = str; + const char *end = str + ARRAY_LENGTH (str) - 1; + double pv; + hb_always_assert (hb_parse_double (&pp, end, &pv)); + hb_always_assert (almost_equal (pv, strtod (str, nullptr))); + hb_always_assert (pp == end); + } + /* n = 251, different mantissa: same decomposition, different leading + * digits. Catches cases where the mantissa's own rounding shifts the + * result across the ULP boundary differently from '.1E251'. */ + { + const char str[] = ".4E251"; + const char *pp = str; + const char *end = str + ARRAY_LENGTH (str) - 1; + double pv; + hb_always_assert (hb_parse_double (&pp, end, &pv)); + hb_always_assert (almost_equal (pv, strtod (str, nullptr))); + hb_always_assert (pp == end); + } + /* n = 293 = 18*16 + 5: _large[18] * _small[5]. + * Fuzzer finding: '9.3E+293' was 3 ULP off. Exercises the top end + * of the large table and a mid-range small offset. */ + { + const char str[] = "9.3E+293"; + const char *pp = str; + const char *end = str + ARRAY_LENGTH (str) - 1; + double pv; + hb_always_assert (hb_parse_double (&pp, end, &pv)); + hb_always_assert (almost_equal (pv, strtod (str, nullptr))); + hb_always_assert (pp == end); + } + /* n = 63 = 3*16 + 15: _large[3] * _small[15]. + * Fuzzer finding: '.0374111E63' was 2 ULP off. Exercises the boundary + * where the small-table offset is at its maximum (15), and a mid-range + * large-table index that the composition path handled with fewer + * multiplications (63 = 32+16+8+4+2+1, six multiplications). */ + { + const char str[] = ".0374111E63"; + const char *pp = str; + const char *end = str + ARRAY_LENGTH (str) - 1; + double pv; + hb_always_assert (hb_parse_double (&pp, end, &pv)); + hb_always_assert (almost_equal (pv, strtod (str, nullptr))); + hb_always_assert (pp == end); + } + /* Bare '1e251' with the trailing garbage from the fuzzer input: + * verifies the longest-prefix path still applies after the _pow10 + * change, and that the returned value is the fuzzer input's target. */ + { + const char str[] = ".1E251-742 E\n2\n262"; + const char *pp = str; + const char *end = str + ARRAY_LENGTH (str) - 1; + double pv; + hb_always_assert (hb_parse_double (&pp, end, &pv)); + hb_always_assert (almost_equal (pv, strtod (str, nullptr))); + hb_always_assert (pp == str + 6); /* ".1E251" is six chars */ + } + /* Sign path: '-1e251' must be the negation of '1e251', and both must + * be within 2 ULP of strtod. Catches cases where the _pow10 change + * interacts with negation. */ + { + const char str[] = "-1e251"; + const char *pp = str; + const char *end = str + ARRAY_LENGTH (str) - 1; + double pv; + hb_always_assert (hb_parse_double (&pp, end, &pv)); + hb_always_assert (almost_equal (pv, strtod (str, nullptr))); + hb_always_assert (pp == end); + } } int