diff --git a/src/main/java/org/apache/commons/collections4/trie/analyzer/StringKeyAnalyzer.java b/src/main/java/org/apache/commons/collections4/trie/analyzer/StringKeyAnalyzer.java index 9552e5a5c8..4f63821ddf 100644 --- a/src/main/java/org/apache/commons/collections4/trie/analyzer/StringKeyAnalyzer.java +++ b/src/main/java/org/apache/commons/collections4/trie/analyzer/StringKeyAnalyzer.java @@ -82,6 +82,9 @@ public int bitIndex(final String key, final int offsetInBits, final int lengthIn k = 0; } else { k = key.charAt(index1); + if (k == 0) { + throw new IllegalArgumentException("Character '\\u0000' is not supported in the key."); + } } if (other == null || index2 >= endIndex2) { diff --git a/src/test/java/org/apache/commons/collections4/trie/PatriciaTrieTest.java b/src/test/java/org/apache/commons/collections4/trie/PatriciaTrieTest.java index 04cb3add70..7dd4032e12 100755 --- a/src/test/java/org/apache/commons/collections4/trie/PatriciaTrieTest.java +++ b/src/test/java/org/apache/commons/collections4/trie/PatriciaTrieTest.java @@ -19,6 +19,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.ConcurrentModificationException; +import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Map; @@ -427,6 +428,24 @@ public void testPrefixMapClearUsingRemove() { assertEquals(Arrays.asList(2, 3, 7, 1), new ArrayList<>(trie.values())); } + public void testNullTerminatedKey() { + // COLLECTIONS-714 + PatriciaTrie trie = new PatriciaTrie<>(); + try { + trie.put("x\u0000", 1); + } catch (IllegalArgumentException e) { + // expected + } + + Map map = new HashMap<>(); + map.put("x\u0000", 1); + try { + trie = new PatriciaTrie<>(map); + } catch (IllegalArgumentException e) { + // expected + } + } + //----------------------------------------------------------------------- @Override