Skip to content

Commit

Permalink
Fix bug in AsYouTypeFormatter where we hit a IndexOutOfBoundsExceptio…
Browse files Browse the repository at this point in the history
…n in its Java implementation when extracting the Chinese national prefix 17951 and didn't reset the format template / lastMatchPosition.

Includes corresponding changes for C++ and JavaScript as well to keep the implementations in sync. (C++ and JS didn't exhibit buggy behavior because the corresponding substring methods don't throw errors for invalid start positions.)

Bugfix for https://github.com/googlei18n/libphonenumber/issues/592
  • Loading branch information
andyst committed Feb 18, 2015
1 parent 3e5f073 commit 49c2427
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cpp/src/phonenumbers/asyoutypeformatter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,9 @@ void AsYouTypeFormatter::AttemptToChoosePatternWithPrefixExtracted(
able_to_format_ = true;
is_expecting_country_code_ = false;
possible_formats_.clear();
last_match_position_ = 0;
formatting_template_.remove();
current_formatting_pattern_.clear();
AttemptToChooseFormattingPattern(formatted_number);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,9 @@ private String attemptToChoosePatternWithPrefixExtracted() {
ableToFormat = true;
isExpectingCountryCallingCode = false;
possibleFormats.clear();
lastMatchPosition = 0;
formattingTemplate.setLength(0);
currentFormattingPattern = "";
return attemptToChooseFormattingPattern();
}

Expand Down Expand Up @@ -637,6 +640,8 @@ private char normalizeAndAccrueDigitsAndPlusSign(char nextChar, boolean remember
}

private String inputDigitHelper(char nextChar) {
// Note that formattingTemplate is not guaranteed to have a value, it could be empty, e.g.
// when the next digit is entered after extracting an IDD or NDD.
Matcher digitMatcher = DIGIT_PATTERN.matcher(formattingTemplate);
if (digitMatcher.find(lastMatchPosition)) {
String tempTemplate = digitMatcher.replaceFirst(Character.toString(nextChar));
Expand Down
3 changes: 3 additions & 0 deletions javascript/i18n/phonenumbers/asyoutypeformatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,9 @@ i18n.phonenumbers.AsYouTypeFormatter.prototype.
this.ableToFormat_ = true;
this.isExpectingCountryCallingCode_ = false;
this.possibleFormats_ = [];
this.lastMatchPosition_ = 0;
this.formattingTemplate_.clear();
this.currentFormattingPattern_ = '';
return this.attemptToChooseFormattingPattern_();
};

Expand Down

0 comments on commit 49c2427

Please sign in to comment.