This is an automated email from the ASF dual-hosted git repository.
garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-text.git
The following commit(s) were added to refs/heads/master by this push:
new f04ebec0 Guard empty split result in DnsStringLookup.lookup (#765)
f04ebec0 is described below
commit f04ebec055370aba39ce747a2a2b60697d8d1ac6
Author: Javid Khan <[email protected]>
AuthorDate: Wed Aug 26 02:05:02 2026 +0530
Guard empty split result in DnsStringLookup.lookup (#765)
---
src/main/java/org/apache/commons/text/lookup/DnsStringLookup.java | 3 +++
.../java/org/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 028879c2..bbc38984 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 @@ final class DnsStringLookup extends AbstractStringLookup {
}
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 b62dee6a..a5c5f252 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 @@ class DnsStringLookupTest {
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));