From d0b45e7221f97c12899b7a27335cb373437ed33f Mon Sep 17 00:00:00 2001 From: Javid Khan Date: Wed, 19 Aug 2026 22:15:17 +0530 Subject: [PATCH] guard empty split result in DnsStringLookup.lookup --- .../org/apache/commons/text/lookup/DnsStringLookup.java | 3 +++ .../apache/commons/text/lookup/DnsStringLookupTest.java | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/src/main/java/org/apache/commons/text/lookup/DnsStringLookup.java b/src/main/java/org/apache/commons/text/lookup/DnsStringLookup.java index 028879c27f..bbc38984de 100644 --- a/src/main/java/org/apache/commons/text/lookup/DnsStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/DnsStringLookup.java @@ -87,6 +87,9 @@ public String lookup(final String key) { } final String[] keys = key.trim().split("\\|"); final int keyLen = keys.length; + if (keyLen == 0) { + return null; + } final String subKey = keys[0].trim(); final String subValue = keyLen < 2 ? key : keys[1].trim(); try { diff --git a/src/test/java/org/apache/commons/text/lookup/DnsStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/DnsStringLookupTest.java index b62dee6aa5..a5c5f25248 100644 --- a/src/test/java/org/apache/commons/text/lookup/DnsStringLookupTest.java +++ b/src/test/java/org/apache/commons/text/lookup/DnsStringLookupTest.java @@ -73,6 +73,14 @@ void testName() throws UnknownHostException { assertTrue(matched); } + @Test + void testDelimiterOnlyKey() { + // A key that is only delimiter/whitespace splits to an empty array; must not throw. + assertNull(DnsStringLookup.INSTANCE.apply("|")); + assertNull(DnsStringLookup.INSTANCE.apply("||")); + assertNull(DnsStringLookup.INSTANCE.apply(" | ")); + } + @Test void testNull() { assertNull(DnsStringLookup.INSTANCE.apply(null));