[Bug]: CharacterFunctions::readDoubleValue overflows an int on a long exponent

Open Beginner friendly
#1,804 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
data

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.

  1. Build juce_core and the program below with -fsanitize=undefined.
  2. 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

  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.