From 7fde83a7e06b2ce035a2de73f4ab6e44e7c5f20b Mon Sep 17 00:00:00 2001 From: ebraminio Date: Sat, 31 Oct 2026 20:18:18 +0330 Subject: [PATCH] [number] Fix two more edge cases (#6529) * [number] Minor rename * [number] Make zero mantissa with large exponent to return 0, not nan * [number] More percise keeping track of fraction --- src/hb-cff-interp-dict-common.hh | 2 +- src/hb-number-parser.hh | 24 ++++++++------ src/hb-number.cc | 2 +- src/test-number.cc | 54 ++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 12 deletions(-) diff --git a/src/hb-cff-interp-dict-common.hh b/src/hb-cff-interp-dict-common.hh index f4b4b18ff..6f335a60e 100644 --- a/src/hb-cff-interp-dict-common.hh +++ b/src/hb-cff-interp-dict-common.hh @@ -105,7 +105,7 @@ struct dict_opset_t : opset_t if (unlikely (nibble == RESERVED)) break; else if (nibble == END) { - size_t length = parser.finish (); + size_t length = parser.commit (); if (unlikely (length == 0 || length != count)) break; return parser.get_value (); diff --git a/src/hb-number-parser.hh b/src/hb-number-parser.hh index df0293591..7bcb3122a 100644 --- a/src/hb-number-parser.hh +++ b/src/hb-number-parser.hh @@ -103,11 +103,10 @@ struct hb_number_parser_t /* Signal end of input. Rolls back a dangling exponent. Returns the * accepted length, or 0 if no valid number was parsed. */ - size_t finish () + HB_NODISCARD size_t commit () { if (state == ExpStart || state == ExpSign) rollback_exponent (); - return (state == Sign || state == AfterDotFromStart || - state == ExpStart || state == ExpSign) ? 0 : pos; + return (state == Sign || state == AfterDotFromStart) ? 0 : pos; } /* Valid after finish() returned true. */ @@ -117,9 +116,13 @@ struct hb_number_parser_t if (!fraction.is_empty ()) v += fraction.get_value (); if (neg) v *= -1.; + /* Zero mantissa: any exponent gives zero, and this avoids + * 0 * HUGE_VAL = nan for "0e441" where _pow10(441) is HUGE_VAL + * but exp_overflow didn't fire. Preserves -0.0 for "-0e<...>". */ + if (v == 0) return v; + if (unlikely (exp_overflow)) { - if (v == 0) return v; /* preserves -0.0 for "-0e" */ if (exp_neg) return neg ? -.0 : .0; else return neg ? -HUGE_VAL : HUGE_VAL; } @@ -144,14 +147,14 @@ struct hb_number_parser_t } private: - static constexpr uint64_t MAX_FRACT = 0xFFFFFFFFFFFFFull; /* 2^52-1 */ + static constexpr uint64_t MAX_FRACT = 10000000000000000000ull; // 10^19 struct /* fraction */ { void add (int digit) { if (likely (fraction <= MAX_FRACT / 10)) { - fraction = fraction * 10. + digit; + fraction = fraction * 10 + digit; ++count; } else if (drop < 0) drop = digit; @@ -160,9 +163,10 @@ private: double get_value () const { - double result = fraction; - /* Round-to-nearest-even using the recorded tail */ - if (drop > 5 || (drop == 5 && ((((uint64_t) fraction) & 1) || sticky))) + /* (double) fraction rounds to nearest; division by an exact + * power of ten rounds once more. Combined error < 1 ULP. */ + double result = (double) fraction; + if (drop > 5 || (drop == 5 && (sticky || (fraction & 1)))) result += 1; return result / _pow10 (count); } @@ -171,7 +175,7 @@ private: private: unsigned count = 0; - double fraction = 0; + uint64_t fraction = 0; int drop = -1; /* drop is the first digit we couldn't accumulate */ bool sticky = false; /* sticky is set when any later digit was nonzero. */ } fraction; diff --git a/src/hb-number.cc b/src/hb-number.cc index bdc63ebae..ebde7e087 100644 --- a/src/hb-number.cc +++ b/src/hb-number.cc @@ -41,7 +41,7 @@ hb_parse_double (const char **pp, const char *end, double *pv, bool whole_buffer hb_number_parser_t parser; while (p < end && parser.consume (*p)) p++; - size_t length = parser.finish (); + size_t length = parser.commit (); if (unlikely (length == 0)) return false; const char *pend = num_start + length; if (unlikely (whole_buffer && pend != end)) return false; diff --git a/src/test-number.cc b/src/test-number.cc index 60969f562..33a313522 100644 --- a/src/test-number.cc +++ b/src/test-number.cc @@ -1905,6 +1905,60 @@ test_parse_double (void) hb_always_assert (pv == strtod (str, nullptr)); hb_always_assert (pp == end); } + /* Zero mantissa, exponent in (308, MAX_EXP]: _pow10 returns HUGE_VAL + * but exp_overflow doesn't fire, so 0 * inf must short-circuit to 0 + * rather than producing nan. */ + { + const char str[] = "0e441"; + 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 (pv == 0.0 && !std::signbit (pv)); + hb_always_assert (pv == strtod (str, nullptr)); + } + { + const char str[] = "-0e441"; + 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 (pv == 0.0 && std::signbit (pv)); + hb_always_assert (pv == strtod (str, nullptr)); + hb_always_assert (std::signbit (pv) == std::signbit (strtod (str, nullptr))); + } + /* Same bug, alternate zero-mantissa forms. */ + { + const char str[] = ".0e441"; + 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 (pv == 0.0 && !std::signbit (pv)); + hb_always_assert (pv == strtod (str, nullptr)); + } + { + const char str[] = "0.e441"; + 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 (pv == 0.0 && !std::signbit (pv)); + hb_always_assert (pv == strtod (str, nullptr)); + } + /* 18 fractional digits: accumulator keeps 15, so we can be ~3 ULP + * off from strtod. Known limit; would tighten to ~1 ULP with a + * uint64_t accumulator. */ + { + const char str[] = ".543588148486817512e"; + 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 (pv > 0.543588148486817 && pv < 0.543588148486818); + hb_always_assert (almost_equal (pv, strtod (str, nullptr))); + hb_always_assert (pp == str + 19); // trailing 'e' not consumed + } } int