This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-codec.git


The following commit(s) were added to refs/heads/master by this push:
     new 29012d70 CODEC-311: Fix possible ArrayIndexOutOfBoundException thrown 
by RefinedSoundex.getMappingCode() (#219)
29012d70 is described below

commit 29012d703d5e079338f9daa9ebceee3ef7b7d93e
Author: Arthur Chan <game...@hotmail.com>
AuthorDate: Sat Nov 25 03:11:52 2023 +0000

    CODEC-311: Fix possible ArrayIndexOutOfBoundException thrown by 
RefinedSoundex.getMappingCode() (#219)
    
    * CODEC-311: Fix possible ArrayIndexOutOfBoundException
    
    Signed-off-by: Arthur Chan <arthur.c...@adalogics.com>
    
    * CODEC-311: Add unit test
    
    Signed-off-by: Arthur Chan <arthur.c...@adalogics.com>
    
    * Use final
    
    ---------
    
    Signed-off-by: Arthur Chan <arthur.c...@adalogics.com>
    Co-authored-by: Gary Gregory <garydgreg...@users.noreply.github.com>
---
 .../java/org/apache/commons/codec/language/RefinedSoundex.java |  6 +++++-
 .../org/apache/commons/codec/language/RefinedSoundexTest.java  | 10 ++++++++++
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git 
a/src/main/java/org/apache/commons/codec/language/RefinedSoundex.java 
b/src/main/java/org/apache/commons/codec/language/RefinedSoundex.java
index 30c7df91..ea0421e9 100644
--- a/src/main/java/org/apache/commons/codec/language/RefinedSoundex.java
+++ b/src/main/java/org/apache/commons/codec/language/RefinedSoundex.java
@@ -173,7 +173,11 @@ public class RefinedSoundex implements StringEncoder {
         if (!Character.isLetter(c)) {
             return 0;
         }
-        return this.soundexMapping[Character.toUpperCase(c) - 'A'];
+        final int index = Character.toUpperCase(c) - 'A';
+        if (index < 0 || index >= this.soundexMapping.length) {
+            return 0;
+        }
+        return this.soundexMapping[index];
     }
 
     /**
diff --git 
a/src/test/java/org/apache/commons/codec/language/RefinedSoundexTest.java 
b/src/test/java/org/apache/commons/codec/language/RefinedSoundexTest.java
index 0494f405..16116b67 100644
--- a/src/test/java/org/apache/commons/codec/language/RefinedSoundexTest.java
+++ b/src/test/java/org/apache/commons/codec/language/RefinedSoundexTest.java
@@ -79,6 +79,16 @@ public class RefinedSoundexTest extends 
AbstractStringEncoderTest<RefinedSoundex
         assertEquals(0, code, "Code does not equals zero");
     }
 
+    @Test
+    public void testInvalidSoundexCharacter() {
+        final char[] invalid = new char[256];
+        for (int i = 0; i < invalid.length; i++) {
+            invalid[i] = (char)i;
+        }
+
+        assertEquals(new RefinedSoundex().encode(new String(invalid)), 
"A0136024043780159360205050136024043780159360205053");
+    }
+
     @Test
     public void testNewInstance() {
         assertEquals("D6043", new RefinedSoundex().soundex("dogs"));

Reply via email to