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-text.git
The following commit(s) were added to refs/heads/master by this push: new cc45196d [TEXT-232] WordUtils.containsAllWords() may throw PatternSyntaxException cc45196d is described below commit cc45196d0815ae41eee66c7f53fd410d30540009 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Thu Jan 25 21:27:35 2024 -0500 [TEXT-232] WordUtils.containsAllWords() may throw PatternSyntaxException --- src/changes/changes.xml | 3 ++ .../java/org/apache/commons/text/WordUtils.java | 2 +- .../org/apache/commons/text/WordUtilsTest.java | 39 ++++++++++++---------- 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 6303d49d..4c76f2a8 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -46,6 +46,9 @@ The <action> type attribute can be add,update,fix,remove. </properties> <body> <release version="1.11.1" date="202Y-MM-DD" description="Release 1.11.1. Requires Java 8 or above."> + <!-- FIX --> + <action issue="TEXT-232" type="fix" dev="ggregory" due-to="Gary Gregory">WordUtils.containsAllWords() may throw PatternSyntaxException.</action> + <!-- UPDATE --> <action type="update" dev="ggregory" due-to="Gary Gregory">Bump commons-lang3 from 3.13.0 to 3.14.0.</action> <action type="update" dev="ggregory" due-to="Dependabot">Bump commons.bytebuddy.version from 1.14.9 to 1.14.11 #476, #482.</action> <action type="update" dev="ggregory" due-to="Dependabot">Bump org.codehaus.mojo:exec-maven-plugin from 3.1.0 to 3.1.1 #474.</action> diff --git a/src/main/java/org/apache/commons/text/WordUtils.java b/src/main/java/org/apache/commons/text/WordUtils.java index 0b1929e6..306c68af 100644 --- a/src/main/java/org/apache/commons/text/WordUtils.java +++ b/src/main/java/org/apache/commons/text/WordUtils.java @@ -281,7 +281,7 @@ public class WordUtils { if (StringUtils.isBlank(w)) { return false; } - final Pattern p = Pattern.compile(".*\\b" + w + "\\b.*"); + final Pattern p = Pattern.compile(".*\\b" + Pattern.quote(w.toString()) + "\\b.*"); if (!p.matcher(word).matches()) { return false; } diff --git a/src/test/java/org/apache/commons/text/WordUtilsTest.java b/src/test/java/org/apache/commons/text/WordUtilsTest.java index 9f1270c4..2a6f9d68 100644 --- a/src/test/java/org/apache/commons/text/WordUtilsTest.java +++ b/src/test/java/org/apache/commons/text/WordUtilsTest.java @@ -18,6 +18,8 @@ package org.apache.commons.text; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.lang.reflect.Constructor; import java.lang.reflect.Modifier; @@ -164,23 +166,26 @@ public class WordUtilsTest { @Test public void testContainsAllWords_StringString() { - assertThat(WordUtils.containsAllWords(null, (String) null)).isFalse(); - assertThat(WordUtils.containsAllWords(null, "")).isFalse(); - assertThat(WordUtils.containsAllWords(null, "ab")).isFalse(); - - assertThat(WordUtils.containsAllWords("", (String) null)).isFalse(); - assertThat(WordUtils.containsAllWords("", "")).isFalse(); - assertThat(WordUtils.containsAllWords("", "ab")).isFalse(); - - assertThat(WordUtils.containsAllWords("foo", (String) null)).isFalse(); - assertThat(WordUtils.containsAllWords("bar", "")).isFalse(); - assertThat(WordUtils.containsAllWords("zzabyycdxx", "by")).isFalse(); - assertThat(WordUtils.containsAllWords("lorem ipsum dolor sit amet", "ipsum", "lorem", "dolor")).isTrue(); - assertThat(WordUtils.containsAllWords("lorem ipsum dolor sit amet", "ipsum", null, "lorem", "dolor")).isFalse(); - assertThat(WordUtils.containsAllWords("lorem ipsum null dolor sit amet", "ipsum", null, "lorem", "dolor")) - .isFalse(); - assertThat(WordUtils.containsAllWords("ab", "b")).isFalse(); - assertThat(WordUtils.containsAllWords("ab", "z")).isFalse(); + assertFalse(WordUtils.containsAllWords(null, (String) null)); + assertFalse(WordUtils.containsAllWords(null, "")); + assertFalse(WordUtils.containsAllWords(null, "ab")); + + assertFalse(WordUtils.containsAllWords("", (String) null)); + assertFalse(WordUtils.containsAllWords("", "")); + assertFalse(WordUtils.containsAllWords("", "ab")); + + assertFalse(WordUtils.containsAllWords("foo", (String) null)); + assertFalse(WordUtils.containsAllWords("bar", "")); + assertFalse(WordUtils.containsAllWords("zzabyycdxx", "by")); + assertTrue(WordUtils.containsAllWords("lorem ipsum dolor sit amet", "ipsum", "lorem", "dolor")); + assertFalse(WordUtils.containsAllWords("lorem ipsum dolor sit amet", "ipsum", null, "lorem", "dolor")); + assertFalse(WordUtils.containsAllWords("lorem ipsum null dolor sit amet", "ipsum", null, "lorem", "dolor")); + assertFalse(WordUtils.containsAllWords("ab", "b")); + assertFalse(WordUtils.containsAllWords("ab", "z")); + assertFalse(WordUtils.containsAllWords("ab", "[")); + assertFalse(WordUtils.containsAllWords("ab", "]")); + assertFalse(WordUtils.containsAllWords("ab", "*")); + assertTrue(WordUtils.containsAllWords("ab x", "ab", "x")); } @Test