From 06aea7e74cfe4a1578cb76672f1562132090c205 Mon Sep 17 00:00:00 2001 From: Stzx Date: Tue, 20 Aug 2019 11:29:55 +0800 Subject: [PATCH 1/2] LANG-1475 Fix unwrap StringIndexOutOfBoundsException When the string length is shorter than two, it should be returned directly without operation. --- .../org/apache/commons/lang3/StringUtils.java | 16 ++++++++++------ .../apache/commons/lang3/StringUtilsTest.java | 4 ++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java index d3388f7a6..c0c463530 100644 --- a/src/main/java/org/apache/commons/lang3/StringUtils.java +++ b/src/main/java/org/apache/commons/lang3/StringUtils.java @@ -9159,8 +9159,10 @@ public class StringUtils { * StringUtils.unwrap(null, null) = null * StringUtils.unwrap(null, '\0') = null * StringUtils.unwrap(null, '1') = null + * StringUtils.unwrap("a", 'a') = "a" + * StringUtils.unwrap("aa", 'a') = "" * StringUtils.unwrap("\'abc\'", '\'') = "abc" - * StringUtils.unwrap("AABabcBAA", 'A') = "ABabcBA" + * StringUtils.unwrap("AABabcBAA", 'A') = "ABabcBA" * StringUtils.unwrap("A", '#') = "A" * StringUtils.unwrap("#A", '#') = "#A" * StringUtils.unwrap("A#", '#') = "A#" @@ -9175,16 +9177,15 @@ public class StringUtils { * @since 3.6 */ public static String unwrap(final String str, final char wrapChar) { - if (isEmpty(str) || wrapChar == CharUtils.NUL) { + if (isEmpty(str) || wrapChar == CharUtils.NUL || str.length() == 1) { return str; } if (str.charAt(0) == wrapChar && str.charAt(str.length() - 1) == wrapChar) { final int startIndex = 0; final int endIndex = str.length() - 1; - if (endIndex != -1) { - return str.substring(startIndex + 1, endIndex); - } + + return str.substring(startIndex + 1, endIndex); } return str; @@ -9199,6 +9200,8 @@ public class StringUtils { * StringUtils.unwrap(null, null) = null * StringUtils.unwrap(null, "") = null * StringUtils.unwrap(null, "1") = null + * StringUtils.unwrap("a", "a") = "a" + * StringUtils.unwrap("aa", "a") = "" * StringUtils.unwrap("\'abc\'", "\'") = "abc" * StringUtils.unwrap("\"abc\"", "\"") = "abc" * StringUtils.unwrap("AABabcBAA", "AA") = "BabcB" @@ -9216,7 +9219,7 @@ public class StringUtils { * @since 3.6 */ public static String unwrap(final String str, final String wrapToken) { - if (isEmpty(str) || isEmpty(wrapToken)) { + if (isEmpty(str) || isEmpty(wrapToken) || str.length() == 1) { return str; } @@ -9224,6 +9227,7 @@ public class StringUtils { final int startIndex = str.indexOf(wrapToken); final int endIndex = str.lastIndexOf(wrapToken); final int wrapLength = wrapToken.length(); + if (startIndex != -1 && endIndex != -1) { return str.substring(startIndex + wrapLength, endIndex); } diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java index 3810a5c12..b2e80e62a 100644 --- a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java @@ -3073,6 +3073,8 @@ public class StringUtilsTest { assertNull(StringUtils.unwrap(null, '1')); assertEquals("abc", StringUtils.unwrap("abc", null)); + assertEquals("a", StringUtils.unwrap("a", "a")); + assertEquals("", StringUtils.unwrap("aa", "a")); assertEquals("abc", StringUtils.unwrap("\'abc\'", '\'')); assertEquals("abc", StringUtils.unwrap("AabcA", 'A')); assertEquals("AabcA", StringUtils.unwrap("AAabcAA", 'A')); @@ -3090,6 +3092,8 @@ public class StringUtilsTest { assertEquals("abc", StringUtils.unwrap("abc", null)); assertEquals("abc", StringUtils.unwrap("abc", "")); + assertEquals("a", StringUtils.unwrap("a", "a")); + assertEquals("", StringUtils.unwrap("aa", "a")); assertEquals("abc", StringUtils.unwrap("\'abc\'", "\'")); assertEquals("abc", StringUtils.unwrap("\"abc\"", "\"")); assertEquals("abc\"xyz", StringUtils.unwrap("\"abc\"xyz\"", "\"")); From 672025e0bf266631a2398cf23122cb1c85b56fb5 Mon Sep 17 00:00:00 2001 From: Stzx Date: Fri, 23 Aug 2019 09:53:43 +0800 Subject: [PATCH 2/2] Supplement changes.xml --- src/changes/changes.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 4535fa979..b78dc7ad5 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -61,6 +61,7 @@ The type attribute can be add,update,fix,remove. checkstyle.version 8.18 -> 8.23. junit-jupiter 5.5.0 -> 5.5.1. Added Functions.as*, and tests thereof, as suggested by Peter Verhas + StringUtils.unwrap incorrect throw StringIndexOutOfBoundsException.