Skip to content

Commit 20002f7

Browse files
committed
Restricted quote stripping to values containing exactly 2 quotes, one at the beginning and one at the end (CLI-185)
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/trunk@955156 13f79535-47bb-0310-9956-ffa450edef68
1 parent a2cf820 commit 20002f7

2 files changed

Lines changed: 8 additions & 6 deletions

File tree

src/java/org/apache/commons/cli/Util.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,12 @@ else if (str.startsWith("-"))
6262
*/
6363
static String stripLeadingAndTrailingQuotes(String str)
6464
{
65-
if (str.startsWith("\""))
65+
int length = str.length();
66+
if (length > 1 && str.startsWith("\"") && str.endsWith("\"") && str.substring(1, length - 1).indexOf('"') == -1)
6667
{
67-
str = str.substring(1, str.length());
68-
}
69-
if (str.endsWith("\""))
70-
{
71-
str = str.substring(0, str.length() - 1);
68+
str = str.substring(1, length - 1);
7269
}
70+
7371
return str;
7472
}
7573
}

src/test/org/apache/commons/cli/UtilTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,9 @@ public void testStripLeadingHyphens()
3535
public void testStripLeadingAndTrailingQuotes()
3636
{
3737
assertEquals("foo", Util.stripLeadingAndTrailingQuotes("\"foo\""));
38+
assertEquals("foo \"bar\"", Util.stripLeadingAndTrailingQuotes("foo \"bar\""));
39+
assertEquals("\"foo\" bar", Util.stripLeadingAndTrailingQuotes("\"foo\" bar"));
40+
assertEquals("\"foo\" and \"bar\"", Util.stripLeadingAndTrailingQuotes("\"foo\" and \"bar\""));
41+
assertEquals("\"", Util.stripLeadingAndTrailingQuotes("\""));
3842
}
3943
}

0 commit comments

Comments
 (0)