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
5 changes: 4 additions & 1 deletion cpp/src/phonenumbers/phonenumbermatcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@ bool AllNumberGroupsRemainGrouped(
if (number.country_code_source() != PhoneNumber::FROM_DEFAULT_COUNTRY) {
// First skip the country code if the normalized candidate contained it.
string country_code = SimpleItoa(number.country_code());
from_index = normalized_candidate.find(country_code) + country_code.size();
size_t country_code_index = normalized_candidate.find(country_code);
if (country_code_index != string::npos) {
from_index = country_code_index + country_code.size();
}
}
// Check each group of consecutive digits are not broken into separate
// groupings in the normalized_candidate string.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,10 @@ static boolean allNumberGroupsRemainGrouped(PhoneNumberUtil util,
if (number.getCountryCodeSource() != CountryCodeSource.FROM_DEFAULT_COUNTRY) {
// First skip the country code if the normalized candidate contained it.
String countryCode = Integer.toString(number.getCountryCode());
fromIndex = normalizedCandidate.indexOf(countryCode) + countryCode.length();
int countryCodeIndex = normalizedCandidate.indexOf(countryCode);
if (countryCodeIndex >= 0) {
fromIndex = countryCodeIndex + countryCode.length();
}
}
// Check each group of consecutive digits are not broken into separate groupings in the
// {@code normalizedCandidate} string.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ public void testContainsMoreThanOneSlashInNationalNumber() throws Exception {
assertTrue(PhoneNumberMatcher.containsMoreThanOneSlashInNationalNumber(number, candidate));
}

public void testAllNumberGroupsRemainGroupedCountryCodeNotInCandidate() throws Exception {
// Constructed case: the country code source claims the candidate began with an explicit
// country calling code, but the literal digits of that code ("44") do not actually occur in
// the candidate text. Previously this desynced fromIndex (indexOf returns -1, and -1 plus the
// country code's length was used as a starting position instead of being treated as "not
// found"), which made a grouping match that should succeed incorrectly return false.
PhoneNumber number = new PhoneNumber();
number.setCountryCode(44);
number.setCountryCodeSource(CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN);
StringBuilder candidate = new StringBuilder("020 7031 3000");
String[] formattedNumberGroups = {"020", "7031", "3000"};
assertTrue(PhoneNumberMatcher.allNumberGroupsRemainGrouped(
phoneUtil, number, candidate, formattedNumberGroups));
}

/** See {@link PhoneNumberUtilTest#testParseNationalNumber()}. */
public void testFindNationalNumber() throws Exception {
// same cases as in testParseNationalNumber
Expand Down