diff options
author | rouslan@chromium.org <rouslan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-07-16 21:22:10 +0000 |
---|---|---|
committer | rouslan@chromium.org <rouslan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-07-16 21:22:10 +0000 |
commit | 02bc9a537ca148fac7cfb0f1b4b7caa5b92f87db (patch) | |
tree | 587f836e069d235f8fca106ed4be71295c2fdce2 | |
parent | 82d45e19654501e4fbb4d7d60d554c14c84dc278 (diff) | |
download | chromium_src-02bc9a537ca148fac7cfb0f1b4b7caa5b92f87db.zip chromium_src-02bc9a537ca148fac7cfb0f1b4b7caa5b92f87db.tar.gz chromium_src-02bc9a537ca148fac7cfb0f1b4b7caa5b92f87db.tar.bz2 |
Merge 211197 "Handle null char in the middle of text in custom s..."
A second attempt to merge this CL into M28 (branch 1500). This time with
utf_string_conversions.h include. Lack of this include blocked the previous
attempt to merge.
> Handle null char in the middle of text in custom spellcheck dictionary engine
>
> The code in custom spellcheck dictionary erroneously assumes that null char
> terminates the string. Other code does not make this assumption, however, and
> passes misspelling offsets to custom spellcheck dictionary that are past the
> position of the null char. This causes an exception. The fix is to not convert
> the string into a char array and then back into a string in custom spellcheck
> dictionary engine.
>
> TEST=CustomDictionaryTest.HandlesNullCharacters
> TEST=CustomDictionaryTest.HandlesEmptyWordWithInvalidSubstring
> BUG=258550
>
> Review URL: https://chromiumcodereview.appspot.com/18137008
TBR=rouslan@chromium.org
Review URL: https://codereview.chromium.org/19278019
git-svn-id: svn://svn.chromium.org/chrome/branches/1500/src@211847 0039d316-1c4b-4281-b951-d872f2087c98
4 files changed, 24 insertions, 13 deletions
diff --git a/chrome/renderer/spellchecker/custom_dictionary_engine.cc b/chrome/renderer/spellchecker/custom_dictionary_engine.cc index 06c4abd..35e1acf 100644 --- a/chrome/renderer/spellchecker/custom_dictionary_engine.cc +++ b/chrome/renderer/spellchecker/custom_dictionary_engine.cc @@ -42,18 +42,14 @@ void CustomDictionaryEngine::OnCustomDictionaryChanged( } bool CustomDictionaryEngine::SpellCheckWord( - const char16* text, + const string16& text, int misspelling_start, int misspelling_len) { - DCHECK(text); - string16 text16(text); - // The text to be checked is empty on OSX(async) right now. // TODO(groby): Fix as part of async hook-up. (http://crbug.com/178241) - if (text16.empty()) - return false; - DCHECK(text16.length() >= size_t(misspelling_start + misspelling_len)); - return misspelling_start >= 0 && + return + misspelling_start >= 0 && misspelling_len > 0 && - dictionary_.count(text16.substr(misspelling_start, misspelling_len)) > 0; + size_t(misspelling_start + misspelling_len) <= text.length() && + dictionary_.count(text.substr(misspelling_start, misspelling_len)) > 0; } diff --git a/chrome/renderer/spellchecker/custom_dictionary_engine.h b/chrome/renderer/spellchecker/custom_dictionary_engine.h index d2dc8ca..d24bdb4 100644 --- a/chrome/renderer/spellchecker/custom_dictionary_engine.h +++ b/chrome/renderer/spellchecker/custom_dictionary_engine.h @@ -25,7 +25,7 @@ class CustomDictionaryEngine { // Spellcheck |text|. Assumes that another spelling engine has set // |misspelling_start| and |misspelling_len| to indicate a misspelling. // Returns true if there are no misspellings, otherwise returns false. - bool SpellCheckWord(const char16* text, + bool SpellCheckWord(const string16& text, int misspelling_start, int misspelling_len); diff --git a/chrome/renderer/spellchecker/custom_dictionary_engine_unittest.cc b/chrome/renderer/spellchecker/custom_dictionary_engine_unittest.cc index 440d110..986e67c 100644 --- a/chrome/renderer/spellchecker/custom_dictionary_engine_unittest.cc +++ b/chrome/renderer/spellchecker/custom_dictionary_engine_unittest.cc @@ -1,8 +1,9 @@ // Copyright (c) 2013 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "chrome/renderer/spellchecker/custom_dictionary_engine.h" +#include "base/strings/utf_string_conversions.h" +#include "chrome/renderer/spellchecker/custom_dictionary_engine.h" #include "testing/gtest/include/gtest/gtest.h" TEST(CustomDictionaryTest, HandlesEmptyWordWithInvalidSubstring) { @@ -13,3 +14,16 @@ TEST(CustomDictionaryTest, HandlesEmptyWordWithInvalidSubstring) { EXPECT_FALSE(engine.SpellCheckWord(string16().c_str(), 15, 23)); } +TEST(CustomDictionaryTest, Basic) { + CustomDictionaryEngine engine; + EXPECT_FALSE(engine.SpellCheckWord(ASCIIToUTF16("helllo").c_str(), 0, 6)); + std::set<std::string> custom_words; + custom_words.insert("helllo"); + engine.Init(custom_words); + EXPECT_TRUE(engine.SpellCheckWord(ASCIIToUTF16("helllo").c_str(), 0, 6)); +} + +TEST(CustomDictionaryTest, HandlesNullCharacters) { + char16 data[4] = {'a', 0, 'b', 'c'}; + EXPECT_FALSE(CustomDictionaryEngine().SpellCheckWord(data, 1, 1)); +} diff --git a/chrome/renderer/spellchecker/spellcheck.cc b/chrome/renderer/spellchecker/spellcheck.cc index b12e5b7..57108f6 100644 --- a/chrome/renderer/spellchecker/spellcheck.cc +++ b/chrome/renderer/spellchecker/spellcheck.cc @@ -223,7 +223,7 @@ bool SpellCheck::SpellCheckParagraph( } if (!custom_dictionary_.SpellCheckWord( - &text[offset], misspelling_start, misspelling_length)) { + text, misspelling_start + offset, misspelling_length)) { string16 replacement; textcheck_results.push_back(WebTextCheckingResult( WebKit::WebTextCheckingTypeSpelling, @@ -368,7 +368,8 @@ void SpellCheck::CreateTextCheckingResults( type = WebKit::WebTextCheckingTypeGrammar; } } - if (!custom_dictionary_.SpellCheckWord(text, word_location, word_length)) { + if (!custom_dictionary_.SpellCheckWord( + line_text, word_location, word_length)) { list.push_back(WebTextCheckingResult( type, word_location + line_offset, |