From 5fc5a8609f41405bbb3581f7f99b5b2aa2e8d022 Mon Sep 17 00:00:00 2001 From: dota17 Date: Wed, 30 Oct 2019 17:58:24 +0800 Subject: [PATCH 1/3] For fix issue 714 --- .vscode/launch.json | 48 +++++++++++++++++++ .../trie/analyzer/StringKeyAnalyzer.java | 3 ++ .../collections4/trie/PatriciaTrieTest.java | 19 ++++++++ 3 files changed, 70 insertions(+) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000000..79283e9e05 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,48 @@ +{ + // 使用 IntelliSense 了解相关属性。 + // 悬停以查看现有属性的描述。 + // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Java", + "type": "java", + "request": "launch", + "stopOnEntry": true, + "jdkPath": "${env:JAVA_HOME}/bin", + "cwd": "${fileDirname}", + "startupClass": "org.apache.commons.collections4.trie.PatriciaTrieTest", + "classpath": [ + ".", + "${fileDirname}" + ] + }, + { + "name": "Java Console App", + "type": "java", + "request": "launch", + "stopOnEntry": true, + "jdkPath": "${env:JAVA_HOME}/bin", + "cwd": "${fileDirname}", + "startupClass": "${fileBasenameNoExtension}", + "classpath": [ + ".", + "${fileDirname}" + ], + "externalConsole": true + }, + { + "type": "java", + "name": "Debug (Launch) - Current File", + "request": "launch", + "mainClass": "${file}" + }, + { + "type": "java", + "name": "Debug (Launch)-MapPerformance", + "request": "launch", + "mainClass": "org.apache.commons.collections4.MapPerformance", + "projectName": "commons-collections4" + } + ] +} \ No newline at end of file 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..03bef41e55 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("Don't support '\\u0000' 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 From 3068248408821283e4e3c63625d56aae45e9fe42 Mon Sep 17 00:00:00 2001 From: dota17 Date: Wed, 30 Oct 2019 18:02:14 +0800 Subject: [PATCH 2/3] For fix issue 714 --- .vscode/launch.json | 48 --------------------------------------------- 1 file changed, 48 deletions(-) delete mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index 79283e9e05..0000000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,48 +0,0 @@ -{ - // 使用 IntelliSense 了解相关属性。 - // 悬停以查看现有属性的描述。 - // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - { - "name": "Java", - "type": "java", - "request": "launch", - "stopOnEntry": true, - "jdkPath": "${env:JAVA_HOME}/bin", - "cwd": "${fileDirname}", - "startupClass": "org.apache.commons.collections4.trie.PatriciaTrieTest", - "classpath": [ - ".", - "${fileDirname}" - ] - }, - { - "name": "Java Console App", - "type": "java", - "request": "launch", - "stopOnEntry": true, - "jdkPath": "${env:JAVA_HOME}/bin", - "cwd": "${fileDirname}", - "startupClass": "${fileBasenameNoExtension}", - "classpath": [ - ".", - "${fileDirname}" - ], - "externalConsole": true - }, - { - "type": "java", - "name": "Debug (Launch) - Current File", - "request": "launch", - "mainClass": "${file}" - }, - { - "type": "java", - "name": "Debug (Launch)-MapPerformance", - "request": "launch", - "mainClass": "org.apache.commons.collections4.MapPerformance", - "projectName": "commons-collections4" - } - ] -} \ No newline at end of file From 659eb8a83b653589ba076ed906abe11b1f1f80d1 Mon Sep 17 00:00:00 2001 From: dota17 Date: Mon, 4 Nov 2019 14:39:41 +0800 Subject: [PATCH 3/3] modify the message of exception --- .../commons/collections4/trie/analyzer/StringKeyAnalyzer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 03bef41e55..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 @@ -83,7 +83,7 @@ public int bitIndex(final String key, final int offsetInBits, final int lengthIn } else { k = key.charAt(index1); if (k == 0) { - throw new IllegalArgumentException("Don't support '\\u0000' in the key."); + throw new IllegalArgumentException("Character '\\u0000' is not supported in the key."); } }