[Bug]: CharacterFunctions::readDoubleValue misreads a number of 1018 or more digits before the decimal point with no exponent part

Open Beginner friendly
#1,805 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
cpp
Domain
backend

Research direction

The fault is in CharacterFunctions::readDoubleValue in modules/juce_core/text/juce_CharacterFunctions.h, where extraExponent is written without a range check in the branch with no exponent part (around L400-L404). Start by reading how the exponent branch already checks the range, then compare the no-exponent path. The test is the infinities block in modules/juce_core/text/juce_CharacterFunctions.cpp. Done means the 1100-nines input reads as inf and the Text category tests pass.

Written by the indexing model from the issue text.

Description

Detailed steps on how to reproduce the bug

CharacterFunctions::readDoubleValue keeps 18 significant digits and counts each further digit before the decimal point in extraExponent (juce_CharacterFunctions.h L300-L304). When the text has an exponent part, the sum of extraExponent and the exponent part is range-checked before it is written (L385-L396). When it has none, extraExponent goes to writeExponentDigits unchecked (L400-L404), which writes three characters, the first '0' + exponent / 100 as a char (L334-L347). From 1000 on, so for a number of 1018 or more digits before the decimal point, that character is not a digit ('0' + 10 is ':'), strtod stops at the e, and the result is the 18 kept digits alone. The char also wraps, so a longer number can read larger or smaller than its kept digits, and some lengths read inf by chance: 25,119 nines are written with e+01, 25,320 nines with e-02, 25,909 nines with e291.

String::getDoubleValue and getFloatValue read through it, and so do JSON::parse for a number with a decimal point and Expression for a constant.

  1. Build juce_core and the program below.
  2. Run it.
#include <juce_core/juce_core.h>
#include <cstdio>
#include <string>
#include <utility>

int main()
{
    const std::pair<const char*, std::string> cases[] {
        { "1017 nines",          std::string (1017, '9') },
        { "1018 nines",          std::string (1018, '9') },
        { "1100 nines",          std::string (1100, '9') },
        { "1100 nines, then e0", std::string (1100, '9') + "e0" },
        { "1100 nines, then .5", std::string (1100, '9') + ".5" },
        { "1, then 1099 zeros",  "1" + std::string (1099, '0') },
        { "25119 nines",         std::string (25119, '9') },
        { "25320 nines",         std::string (25320, '9') },
    };

    for (const auto& [label, text] : cases)
        std::printf ("%-24s %g\n", label, juce::String (text).getDoubleValue());

    const auto json = juce::JSON::parse ("[" + std::string (1100, '9') + ".0]");
    std::printf ("%-24s %g\n", "JSON, 1100 nines.0", (double) json[0]);

    juce::String parseError;
    const juce::Expression expression (std::string (1100, '9'), parseError);
    std::printf ("%-24s %g\n", "Expression, 1100 nines", expression.evaluate());

    return 0;
}

Output on develop b5d9b21c8 (macOS 26.7.2, arm64, Apple clang 21):

1017 nines               inf
1018 nines               1e+18
1100 nines               1e+18
1100 nines, then e0      inf
1100 nines, then .5      1e+18
1, then 1099 zeros       1e+17
25119 nines              1e+19
25320 nines              1e+16
JSON, 1100 nines.0       1e+18
Expression, 1100 nines   1e+18

The same digits read as inf once e0 follows them, and 1017 nines read as inf too; only a number with no exponent part and an extraExponent of 1000 or more goes wrong. The code is the same on every platform, and the result does not depend on whether char is signed; I ran it on macOS only.

A possible fix is the range check the exponent branch already has, in the branch with no exponent part. With the change below every line above prints inf. The diff also adds 1100 nines to the readDoubleValue test's infinities: without the fix that case fails 267 times across the prefixes, suffixes and character types the test uses, 178 of them inf read as 1e+18, and the 89 with the - prefix -inf read as -1e+18; with it, the Text category's tests pass with no failures. It touches different lines from the fix suggested in #1804, and the two apply together.

The diff as a file, with the sources' CRLF line endings: https://gist.github.com/nkratochwill/238a1fc4584cd078f0b40b7a7d633358

Diff against develop b5d9b21c8 (this copy applies with git apply --ignore-whitespace)
diff --git a/modules/juce_core/text/juce_CharacterFunctions.cpp b/modules/juce_core/text/juce_CharacterFunctions.cpp
index 3e5162071..6461c3f17 100644
--- a/modules/juce_core/text/juce_CharacterFunctions.cpp
+++ b/modules/juce_core/text/juce_CharacterFunctions.cpp
@@ -509,7 +509,8 @@ public:
         }
 
         {
-            std::vector<const char*> infCharPtrs = { "Inf", "inf", "INF", "InF", "1.0E1024", "1.23456789012345678901234567890e123456789" };
+            const std::string manyDigits (1100, '9');
+            std::vector<const char*> infCharPtrs = { "Inf", "inf", "INF", "InF", "1.0E1024", "1.23456789012345678901234567890e123456789", manyDigits.c_str() };
             auto infs = asciiToMemoryBlocks (infCharPtrs, true);
 
             withAllPrefixesAndSuffixes (prefixes, separators.terminals, infs, [this] (const MemoryBlock& data,
diff --git a/modules/juce_core/text/juce_CharacterFunctions.h b/modules/juce_core/text/juce_CharacterFunctions.h
index 55024acbf..99a6d297a 100644
--- a/modules/juce_core/text/juce_CharacterFunctions.h
+++ b/modules/juce_core/text/juce_CharacterFunctions.h
@@ -399,6 +399,9 @@ public:
         }
         else if (extraExponent > 0)
         {
+            if (extraExponent > std::numeric_limits<double>::max_exponent10 + 1)
+                return isNegative ? -inf : inf;
+
             *writePtr++ = 'e';
             writeExponentDigits (extraExponent, writePtr);
         }
What is the expected behaviour?

inf, or -inf after a -, for a number of 1018 or more digits before the decimal point, as for 1017 digits or the same digits followed by e0, since its value is far outside the range of a double.

Operating systems

macOS

What versions of the operating systems?

macOS 26.7.2 (25G332)

Architectures

Arm64/aarch64

Stacktrace

No response

Plug-in formats (if applicable)

No response

Plug-in host applications (DAWs) (if applicable)

No response

Testing on the develop branch

The bug is present on the develop branch

Code of Conduct
  • I agree to follow the Code of Conduct
Dominant language
C++
Stars
8.9k
Forks
2.2k
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from juce-framework/JUCE

All issues in juce-framework/JUCE

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.