Skip to content

Commit 29012d7

Browse files
CODEC-311: Fix possible ArrayIndexOutOfBoundException thrown by RefinedSoundex.getMappingCode() (apache#219)
* CODEC-311: Fix possible ArrayIndexOutOfBoundException Signed-off-by: Arthur Chan <arthur.chan@adalogics.com> * CODEC-311: Add unit test Signed-off-by: Arthur Chan <arthur.chan@adalogics.com> * Use final --------- Signed-off-by: Arthur Chan <arthur.chan@adalogics.com> Co-authored-by: Gary Gregory <garydgregory@users.noreply.github.com>
1 parent 35f0f31 commit 29012d7

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,11 @@ char getMappingCode(final char c) {
173173
if (!Character.isLetter(c)) {
174174
return 0;
175175
}
176-
return this.soundexMapping[Character.toUpperCase(c) - 'A'];
176+
final int index = Character.toUpperCase(c) - 'A';
177+
if (index < 0 || index >= this.soundexMapping.length) {
178+
return 0;
179+
}
180+
return this.soundexMapping[index];
177181
}
178182

179183
/**

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ public void testGetMappingCodeNonLetter() {
7979
assertEquals(0, code, "Code does not equals zero");
8080
}
8181

82+
@Test
83+
public void testInvalidSoundexCharacter() {
84+
final char[] invalid = new char[256];
85+
for (int i = 0; i < invalid.length; i++) {
86+
invalid[i] = (char)i;
87+
}
88+
89+
assertEquals(new RefinedSoundex().encode(new String(invalid)), "A0136024043780159360205050136024043780159360205053");
90+
}
91+
8292
@Test
8393
public void testNewInstance() {
8494
assertEquals("D6043", new RefinedSoundex().soundex("dogs"));

0 commit comments

Comments
 (0)