Skip to content

Commit 047d247

Browse files
CODEC-312: Fix possible StringIndexOutOfBoundException thrown by MatchRatingApproachEncoder.encode() method (#220)
* CODEC-312: Fix possible StringIndexOutOfBoundException Signed-off-by: Arthur Chan <arthur.chan@adalogics.com> * CODEC-312: Add unit test Signed-off-by: Arthur Chan <arthur.chan@adalogics.com> * Remove unmaintained comments --------- Signed-off-by: Arthur Chan <arthur.chan@adalogics.com> Co-authored-by: Gary Gregory <garydgregory@users.noreply.github.com>
1 parent a2e542d commit 047d247

2 files changed

Lines changed: 24 additions & 14 deletions

File tree

src/main/java/org/apache/commons/codec/language/MatchRatingApproachEncoder.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,20 @@ public final String encode(String name) {
126126
// Preprocessing
127127
name = cleanName(name);
128128

129+
// Bulletproof if name becomes empty after cleanName(name)
130+
if (SPACE.equals(name) || name.isEmpty()) {
131+
return EMPTY;
132+
}
133+
129134
// BEGIN: Actual encoding part of the algorithm...
130135
// 1. Delete all vowels unless the vowel begins the word
131136
name = removeVowels(name);
132137

138+
// Bulletproof if name becomes empty after removeVowels(name)
139+
if (SPACE.equals(name) || name.isEmpty()) {
140+
return EMPTY;
141+
}
142+
133143
// 2. Remove second consonant from any double consonant
134144
name = removeDoubleConsonants(name);
135145

src/test/java/org/apache/commons/codec/language/MatchRatingApproachEncoderTest.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@
3535
*/
3636
public class MatchRatingApproachEncoderTest extends AbstractStringEncoderTest<MatchRatingApproachEncoder> {
3737

38-
// ********** BEGIN REGION - TEST SUPPORT METHODS
39-
4038
@Override
4139
protected MatchRatingApproachEncoder createStringEncoder() {
4240
return new MatchRatingApproachEncoder();
@@ -248,10 +246,6 @@ public final void testCompare_Surname_OSULLIVAN_OSUILLEABHAIN_SuccessfulMatch()
248246
assertTrue(this.getStringEncoder().isEncodeEquals("O'Sullivan", "Ó ' Súilleabháin"));
249247
}
250248

251-
// ***** END REGION - TEST SUPPORT METHODS
252-
253-
// ***** BEGIN REGION - TEST GET MRA ENCODING
254-
255249
@Test
256250
public final void testCompare_Surname_PRZEMYSL_PSHEMESHIL_SuccessfullyMatched() {
257251
assertTrue(this.getStringEncoder().isEncodeEquals(" P rz e m y s l", " P sh e m e sh i l"));
@@ -297,10 +291,6 @@ public final void testCompare_ZACH_ZAKARIA_SuccessfullyMatched() {
297291
assertTrue(this.getStringEncoder().isEncodeEquals("Zach", "Zacharia"));
298292
}
299293

300-
// ***** END REGION - TEST GET MRA ENCODING
301-
302-
// ***** BEGIN REGION - TEST GET MRA COMPARISONS
303-
304294
@Test
305295
public final void testCompareNameNullSpace_ReturnsFalseSuccessfully() {
306296
assertFalse(getStringEncoder().isEncodeEquals(null, " "));
@@ -433,8 +423,6 @@ public final void testIsEncodeEquals_CornerCase_FirstNameNothing_ReturnsFalse()
433423
assertFalse(this.getStringEncoder().isEncodeEquals("", "test"));
434424
}
435425

436-
// **** BEGIN YIDDISH/SLAVIC SECTION ****
437-
438426
@Test
439427
public final void testIsEncodeEquals_CornerCase_FirstNameNull_ReturnsFalse() {
440428
assertFalse(this.getStringEncoder().isEncodeEquals(null, "test"));
@@ -470,8 +458,6 @@ public final void testIsVowel_SingleVowel_ReturnsTrue() {
470458
assertTrue(this.getStringEncoder().isVowel("I"));
471459
}
472460

473-
// **** END YIDDISH/SLAVIC SECTION ****
474-
475461
@Test
476462
public final void testIsVowel_SmallD_ReturnsFalse() {
477463
assertFalse(this.getStringEncoder().isVowel("d"));
@@ -519,4 +505,18 @@ public final void testRemoveVowel_ALESSANDRA_Returns_ALSSNDR() {
519505

520506
// ***** END REGION - TEST GET MRA COMPARISONS
521507

508+
@Test
509+
public final void testPunctuationOnly() {
510+
assertEquals(this.getStringEncoder().encode(".,-"), "");
511+
}
512+
513+
@Test
514+
public final void testVowelOnly() {
515+
assertEquals(this.getStringEncoder().encode("aeiouAEIOU"), "A");
516+
}
517+
518+
@Test
519+
public final void testVowelAndPunctuationOnly() {
520+
assertEquals(this.getStringEncoder().encode("uoiea.,-AEIOU"), "U");
521+
}
522522
}

0 commit comments

Comments
 (0)