Repository navigation
Replace recursive regex in PhoneNumberUtil.normalize() with linear scan - #4094
Open
reginaldalfret wants to merge 1 commit into
Open
reginaldalfret wants to merge 1 commit into
reginaldalfret wants to merge 1 commit into
Conversation
PR google#4017 replaced VALID_ALPHA_PHONE_PATTERN with hasAtLeastThreeAlphaChars() in PhoneNumberUtil.isAlphaNumber() to eliminate StackOverflowError caused by recursive evaluation on long numeric inputs in java.util.regex. However, PhoneNumberUtil.normalize(StringBuilder) still called VALID_ALPHA_PHONE_PATTERN.matcher(number).matches(), leaving normalize() vulnerable to the same recursion failure and leaving VALID_ALPHA_PHONE_PATTERN in the codebase solely for that call. This commit updates normalize(StringBuilder) to use hasAtLeastThreeAlphaChars(number) and removes the unused VALID_ALPHA_PHONE_PATTERN, fully eliminating the pattern and preventing StackOverflowError on long inputs. Also adds a regression test and updates pending_code_changes.txt. Signed-off-by: reginaldalfret <reginaldalfret@gmail.com>
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.
Problem
In PR #4017, VALID_ALPHA_PHONE_PATTERN ((?:.?[A-Za-z]){3}.) was replaced with hasAtLeastThreeAlphaChars() in PhoneNumberUtil.isAlphaNumber() because the non-greedy regex causes recursive stack frame evaluation in java.util.regex, resulting in StackOverflowError on long numeric input strings.
However, PhoneNumberUtil.normalize(StringBuilder) still called:
java Matcher m = VALID_ALPHA_PHONE_PATTERN.matcher(number); if (m.matches()) {leaving
ormalize() vulnerable to the exact same StackOverflowError when called with long numeric inputs (e.g. ~1,684+ digits), and leaving VALID_ALPHA_PHONE_PATTERN in the codebase solely for that one call.
Solution
Verification