Browse Source

Defensive check added to FormatInOriginalFormat (#2294)

* Defensive check added to FormatInOriginalFormat

Add a check that the FG is indeed in the national prefix before erasing it, in FormatInOriginalFormat. Matches java interpretation. It should always be there but if for some reason someone generates metadata with it not there we don't want to seg fault.

* Update phonenumberutil.cc
pull/2296/head
lararennie 7 years ago
committed by GitHub
parent
commit
8a2c598dd7
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 3 deletions
  1. +9
    -3
      cpp/src/phonenumbers/phonenumberutil.cc

+ 9
- 3
cpp/src/phonenumbers/phonenumberutil.cc View File

@ -14,9 +14,9 @@
#include "phonenumbers/phonenumberutil.h"
#include <string.h>
#include <algorithm>
#include <cctype>
#include <cstring>
#include <iterator>
#include <map>
#include <utility>
@ -1426,8 +1426,14 @@ void PhoneNumberUtil::FormatInOriginalFormat(const PhoneNumber& number,
// We assume that the first-group symbol will never be _before_ the
// national prefix.
if (!candidate_national_prefix_rule.empty()) {
candidate_national_prefix_rule.erase(
candidate_national_prefix_rule.find("$1"));
size_t index_of_first_group = candidate_national_prefix_rule.find("$1");
if (index_of_first_group == string::npos) {
LOG(ERROR) << "First group missing in national prefix rule: "
<< candidate_national_prefix_rule;
Format(number, NATIONAL, formatted_number);
break;
}
candidate_national_prefix_rule.erase(index_of_first_group);
NormalizeDigitsOnly(&candidate_national_prefix_rule);
}
if (candidate_national_prefix_rule.empty()) {


Loading…
Cancel
Save