Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,6 @@ public class PhoneNumberUtil {
private static final String UNWANTED_END_CHARS = "[[\\P{N}&&\\P{L}]&&[^#]]+$";
static final Pattern UNWANTED_END_CHAR_PATTERN = Pattern.compile(UNWANTED_END_CHARS);

// We use this pattern to check if the phone number has at least three letters in it - if so, then
// we treat it as a number where some phone-number digits are represented by letters.
private static final Pattern VALID_ALPHA_PHONE_PATTERN = Pattern.compile("(?:.*?[A-Za-z]){3}.*");

// Regular expression of viable phone numbers. This is location independent. Checks we have at
// least three leading digits, and only valid punctuation, alpha characters and
// digits in the phone number. Does not include extension data.
Expand Down Expand Up @@ -802,8 +798,7 @@ static boolean isViablePhoneNumber(CharSequence number) {
* normalized in place
*/
static StringBuilder normalize(StringBuilder number) {
Matcher m = VALID_ALPHA_PHONE_PATTERN.matcher(number);
if (m.matches()) {
if (hasAtLeastThreeAlphaChars(number)) {
number.replace(0, number.length(), normalizeHelper(number, ALPHA_PHONE_MAPPINGS, true));
} else {
number.replace(0, number.length(), normalizeDigitsOnly(number));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,15 @@ public void testNormaliseOtherDigits() {
expectedOutput, PhoneNumberUtil.normalize(inputNumber).toString());
}

public void testNormaliseLongNumericInputDoesNotThrow() {
StringBuilder inputNumber = new StringBuilder("+");
for (int i = 0; i < 5000; i++) {
inputNumber.append('1');
}
// Should not throw StackOverflowError and correctly normalize digits.
assertEquals(5000, PhoneNumberUtil.normalize(inputNumber).length());
}

public void testNormaliseStripAlphaCharacters() {
String inputNumber = "034-56&+a#234";
String expectedOutput = "03456234";
Expand Down
3 changes: 2 additions & 1 deletion pending_code_changes.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@

Code changes:
- Fixed PhoneNumberUtil.normalize(StringBuilder) in Java to use a linear character scan instead of the recursive VALID_ALPHA_PHONE_PATTERN regex, preventing StackOverflowError on long inputs.
Loading