[Bug]: CharacterFunctions::readDoubleValue misreads a number of 1018 or more digits before the decimal point with no exponent part
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 72/100
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.
- Build juce_core and the program below.
- 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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from juce-framework/JUCE
-
[Bug]: VST3 getMidiControllerAssignment reads past midiControllerToParameter for program changeOpen
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
juce-framework/JUCE#1810 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
juce-framework/JUCE#1804 ·
-
[Bug]: Linux: an embedded window's vblank rate comes from the display at its parent-relative boundsOpen
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
juce-framework/JUCE#1800 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
juce-framework/JUCE#1795 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
juce-framework/JUCE#1790 ·
All issues in juce-framework/JUCE
Similar issues
-
ai_p2
Difficulty 2/5 1-3 hours Newbie friendliness 80/100
ClickHouse/ClickHouse#132519 ·
Maintainers usually reply within 1 day
-
patch_advisor_json_integration fails when the build tree is reached through a symbolic linkPossibly taken @DerKarussellbremser claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
boykopovar/AnyPS5#22581 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 82/100
petercorke/robotics-toolbox-python#952 ·
Maintainers usually reply within 1 day
-
NatronRenderer: a Python error in the script (-t / project script) doesn't change the exit statusOpenfunc:engine type:bug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
NatronGitHub/Natron#1276 ·
Maintainers usually reply within 1 day