[Bug]: CharacterFunctions::readDoubleValue overflows an int on a long exponent
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 72/100
Research direction
The exponent loop in CharacterFunctions::readDoubleValue is in modules/juce_core/text/juce_CharacterFunctions.h around lines 369-396; the range checks after the loop are where the result is decided. Build the reproduction program from the issue with juce_core and -fsanitize=undefined, then run it on the listed inputs such as 1e40666666666. Done means the sanitizer reports no overflow and the inputs return inf, or 0 for the negative exponent, while ordinary numbers such as 1e5 still read correctly.
Written by the indexing model from the issue text.
Description
Detailed steps on how to reproduce the bug
CharacterFunctions::readDoubleValue collects the exponent's digits in an int with no bound (juce_CharacterFunctions.h L369-L377). An exponent above 2147483647 overflows it, which is undefined behaviour, before the range checks at L385-L396 can return infinity or zero. String::getDoubleValue, JSON::parse and Expression all read numbers through this function, so a number in a text file is enough. A fuzzer found it with the text 1e40666666666.
- Build juce_core and the program below with
-fsanitize=undefined. - Run it with
UBSAN_OPTIONS=print_stacktrace=1.
#include <juce_core/juce_core.h>
#include <cstdio>
int main()
{
const auto value = juce::String ("1e40666666666").getDoubleValue();
std::printf ("value = %g\n", value);
return 0;
}
Output on develop b5d9b21c8 (macOS 26.7.2, arm64, Apple clang 21), with the stack trace in its own field below:
juce_CharacterFunctions.h:377:42: runtime error: signed integer overflow: 406666666 * 10 cannot be represented in type 'int'
value = inf
Built without the sanitizer (-O2), the int wraps here and the results are wrong:
| Text | Returns | Expected |
|---|---|---|
1e4294967297 |
10 | inf |
1e-4294967297 |
0.1 | 0 |
1e2147483648 |
0 | inf |
The sum at L383 overflows in the same way when digits past the 18 significant ones are added to an exponent that only just fits: 12345678901234567890123e2147483647 reports 383:38: signed integer overflow: 5 + 2147483647 and returns 0.
The code is the same on every platform; I ran it on macOS only.
A possible fix is to stop adding digits once the exponent is far outside the range of a double; the checks after the loop then return infinity or zero. Exponents up to 999999999 are still read in full, so a number that reads correctly today reads the same unless it has 100 million digits or more. With the change below, every text above runs clean under UBSan and returns inf, or 0 for the negative exponent.
The diff as a file, with the sources' CRLF line endings: https://gist.github.com/nkratochwill/00616f2426d38a1f7917ca5cd0c93fc3
Diff against develop b5d9b21c8 (this copy applies with git apply --ignore-whitespace)
--- a/modules/juce_core/text/juce_CharacterFunctions.h
+++ b/modules/juce_core/text/juce_CharacterFunctions.h
@@ -374,5 +374,7 @@ public:
auto digit = (int) text.getAndAdvance() - '0';
- if (digit != 0 || exponent != 0)
+ // Past this the value is far outside the range of a double, and further
+ // digits would overflow the int
+ if ((digit != 0 || exponent != 0) && exponent < 100000000)
exponent = (exponent * 10) + digit;
}
What is the expected behaviour?
inf, or 0 for a negative exponent, with no undefined behaviour.
Operating systems
macOS
What versions of the operating systems?
macOS 26.7.2 (25G332)
Architectures
Arm64/aarch64
Stacktrace
#0 in double juce::CharacterFunctions::readDoubleValue<juce::CharPointer_UTF8>(juce::CharPointer_UTF8&) juce_CharacterFunctions.h:377
#1 in juce::String::getDoubleValue() const juce_String.cpp:1880
#2 in main main.cpp:6
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#1805 ·
-
[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