From 8a2c598dd793c4e6d49d15ff733bdc0925664b03 Mon Sep 17 00:00:00 2001 From: lararennie Date: Fri, 7 Dec 2018 15:51:12 +0100 Subject: [PATCH] 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 --- cpp/src/phonenumbers/phonenumberutil.cc | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/cpp/src/phonenumbers/phonenumberutil.cc b/cpp/src/phonenumbers/phonenumberutil.cc index de0bdda03..cc4632816 100644 --- a/cpp/src/phonenumbers/phonenumberutil.cc +++ b/cpp/src/phonenumbers/phonenumberutil.cc @@ -14,9 +14,9 @@ #include "phonenumbers/phonenumberutil.h" -#include #include #include +#include #include #include #include @@ -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()) {