LUCENE-10533: SpellChecker.formGrams is missing bounds check (#836)

This commit is contained in:
Kevin Risden 2022-04-25 15:55:50 -04:00 committed by GitHub
parent a53d05b9f9
commit 223a74fcb5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 30 additions and 3 deletions

View File

@ -147,6 +147,8 @@ Bug Fixes
* LUCENE-10495: Fix return statement of siblingsLoaded() in TaxonomyFacets. (Yuting Gan)
* LUCENE-10533: SpellChecker.formGrams is missing bounds check (Kevin Risden)
Build
---------------------

View File

@ -426,6 +426,9 @@ public class SpellChecker implements java.io.Closeable {
*/
private static String[] formGrams(String text, int ng) {
int len = text.length();
if (len < ng) {
return new String[] {};
}
String[] res = new String[len - ng + 1];
for (int i = 0; i < len - ng + 1; i++) {
res[i] = text.substring(i, i + ng);
@ -559,7 +562,7 @@ public class SpellChecker implements java.io.Closeable {
if (l == 5) {
return 3;
}
return 2;
return Math.min(l, 2);
}
private static Document createDocument(String text, int ng1, int ng2) {

View File

@ -187,7 +187,9 @@ public class TestLuceneDictionary extends LuceneTestCase {
indexReader = DirectoryReader.open(store);
sc.indexDictionary(
new LuceneDictionary(indexReader, "contents"), newIndexWriterConfig(null), false);
String[] suggestions = sc.suggestSimilar("Tam", 1);
String[] suggestions = sc.suggestSimilar("", 1);
assertEquals(0, suggestions.length);
suggestions = sc.suggestSimilar("Tam", 1);
assertEquals(1, suggestions.length);
assertEquals("Tom", suggestions[0]);
suggestions = sc.suggestSimilar("Jarry", 1);

View File

@ -31,7 +31,9 @@ public class TestPlainTextDictionary extends LuceneTestCase {
Directory ramDir = newDirectory();
SpellChecker spellChecker = new SpellChecker(ramDir);
spellChecker.indexDictionary(ptd, newIndexWriterConfig(null), false);
String[] similar = spellChecker.suggestSimilar("treeword", 2);
String[] similar = spellChecker.suggestSimilar("", 2);
assertEquals(0, similar.length);
similar = spellChecker.suggestSimilar("treeword", 2);
assertEquals(2, similar.length);
assertEquals(similar[0], "threeword");
assertEquals(similar[1], "oneword");

View File

@ -194,6 +194,12 @@ public class TestSpellChecker extends LuceneTestCase {
spellChecker.clearIndex();
addwords(r, spellChecker, "field1");
{
String[] similar =
spellChecker.suggestSimilar("", 2, r, "field1", SuggestMode.SUGGEST_WHEN_NOT_IN_INDEX);
assertEquals(0, similar.length);
}
{
String[] similar =
spellChecker.suggestSimilar(
@ -210,6 +216,12 @@ public class TestSpellChecker extends LuceneTestCase {
assertEquals("eight", similar[0]);
}
{
String[] similar =
spellChecker.suggestSimilar("", 5, r, "field1", SuggestMode.SUGGEST_MORE_POPULAR);
assertEquals(0, similar.length);
}
{
String[] similar =
spellChecker.suggestSimilar("eighty", 5, r, "field1", SuggestMode.SUGGEST_MORE_POPULAR);
@ -230,6 +242,12 @@ public class TestSpellChecker extends LuceneTestCase {
assertEquals(0, similar.length);
}
{
String[] similar =
spellChecker.suggestSimilar("", 5, r, "field1", SuggestMode.SUGGEST_ALWAYS);
assertEquals(0, similar.length);
}
{
String[] similar =
spellChecker.suggestSimilar("eighty", 5, r, "field1", SuggestMode.SUGGEST_ALWAYS);