Replace recursive regex with linear scan in isAlphaNumber() to fix StackOverflowError#4017
Open
bann6578-boop wants to merge 2 commits into
Open
Replace recursive regex with linear scan in isAlphaNumber() to fix StackOverflowError#4017bann6578-boop wants to merge 2 commits into
bann6578-boop wants to merge 2 commits into
Conversation
…t StackOverflowError
isAlphaNumber() did not limit input length before evaluating
VALID_ALPHA_PHONE_PATTERN, allowing a numeric-only input of ~1684
characters to cause an uncaught StackOverflowError via unbounded
regex recursion. Unlike parse(), which is guarded by
MAX_INPUT_STRING_LENGTH, this method had no such bound.
This adds the same MAX_INPUT_STRING_LENGTH guard already used in
parseHelper() as defense-in-depth, and additionally replaces the
recursive regex VALID_ALPHA_PHONE_PATTERN with an equivalent linear
character scan (hasAtLeastThreeAlphaChars), eliminating the
recursion-based failure mode at its root rather than only guarding
against it. The replacement matches the exact ASCII [A-Za-z] range
of the original pattern, so behavior is unchanged for all valid
inputs.
Verified:
- Input up to 50,000 characters no longer throws StackOverflowError
- isAlphaNumber("1800MICROSOFT"), "325-CARS", "0800 REPAIR",
"1-800-MY-APPLE" all still return true (no behavior change)
- isAlphaNumber("+14155552671") still returns false
- Full PhoneNumberUtilTest suite passes: 121 tests, 0 failures, 0 errors
Related: Google OSS VRP / Buganizer issue 525896790
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Patch Proposal: Replace recursive regex in PhoneNumberUtil.isAlphaNumber()
Context
"PhoneNumberUtil.isAlphaNumber() lacks an input length limit. A specially crafted numeric string of approximately 1,684 characters triggers recursive regex evaluation, causing a StackOverflowError and terminating the calling thread."
Therefore, this PR is submitted as a direct patch following the second approach.
Issue
The isAlphaNumber() function does not check the length of the input before evaluation VALID_ALPHA_PHONE_PATTERN ((?:.?[A-Za-z]){3}.). A string containing approximately 1,684 numeric characters can cause deep recursive evaluation in java.util.regex, resulting in a StackOverflowError, which has been confirmed on JDK 21 and JDK 25.
and JDK 25, in two separate commits (593719a (June 18, 2026), 5f4c9f0(2026-6-19), one day apart.
Proposed Solution: Completely Remove the Regular Expression
Instead of simply safeguarding the existing regrex expression by checking its length, this PR
replaces VALID_ALPHA_PHONE_PATTERN with an equivalent linear character-scanning algorithm.
This eliminates the root cause of errors caused by recursive evaluation,
regardless of any length constraints:
Equivalence proof
VALID_ALPHA_PHONE_PATTERN.matches() returns true if and only if the input contains at least 3 characters that match [A-Za-z] (the (?:.?[A-Za-z]){3} part requires 3 such characters, in any position, with arbitrary characters allowed to appear between or around them via the ".?" and the ".*"part at the end). In fact, this is exactly what directly counting the number of alphabetic characters checks for. It uses the same ASCII range as the base character set [A-Za-z].
Therefore, the behavior is the same for all inputs, not just equivalent.
This fix:
-Is faster than the regex for all inputs (single linear scan vs. regex engine overhead).
Optional, secondary: input length guard
hasAtLeastThreeAlphaChars()function does not involve recursion or backtracking regardless of the input length. I leave it to the maintainer to decide whether to add this limit to ensure consistency with the class’s other input-handling conventions:public boolean isAlphaNumber(CharSequence number) { + if (number.length() > MAX_INPUT_STRING_LENGTH) { + return false; + } if (!isViablePhoneNumber(number)) {Verification
consisting only of digits still returns false correctly.
The full official test suite passed after the fix:
Proposed Additional Tests
To capture both the fixes and the unchanged behavior in the test suite
(location:
java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java):