You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2025/10/29 11:35:02 UTC
(commons-exec) branch master updated: fix quoteArgument (#309)
This is an automated email from the ASF dual-hosted git repository.
sebb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-exec.git
The following commit(s) were added to refs/heads/master by this push:
new d9e57f35 fix quoteArgument (#309)
d9e57f35 is described below
commit d9e57f352ca2ca2ca843ddea85f48c8929bdd2b5
Author: Xin Wang <be...@163.com>
AuthorDate: Wed Oct 29 19:34:57 2025 +0800
fix quoteArgument (#309)
---
.../java/org/apache/commons/exec/util/StringUtils.java | 10 +++++-----
.../java/org/apache/commons/exec/util/StringUtilTest.java | 15 +++++++++++++++
2 files changed, 20 insertions(+), 5 deletions(-)
diff --git a/src/main/java/org/apache/commons/exec/util/StringUtils.java b/src/main/java/org/apache/commons/exec/util/StringUtils.java
index 1f689cd6..182fae4a 100644
--- a/src/main/java/org/apache/commons/exec/util/StringUtils.java
+++ b/src/main/java/org/apache/commons/exec/util/StringUtils.java
@@ -87,13 +87,13 @@ public class StringUtils {
String cleanedArgument = argument.trim();
- // strip the quotes from both ends
- while (cleanedArgument.startsWith(SINGLE_QUOTE) || cleanedArgument.startsWith(DOUBLE_QUOTE)) {
- cleanedArgument = cleanedArgument.substring(1);
+ // strip the quotes from both starts and ends
+ while (cleanedArgument.startsWith(SINGLE_QUOTE) && cleanedArgument.endsWith(SINGLE_QUOTE)) {
+ cleanedArgument = cleanedArgument.substring(1, cleanedArgument.length() - 1);
}
- while (cleanedArgument.endsWith(SINGLE_QUOTE) || cleanedArgument.endsWith(DOUBLE_QUOTE)) {
- cleanedArgument = cleanedArgument.substring(0, cleanedArgument.length() - 1);
+ while (cleanedArgument.startsWith(DOUBLE_QUOTE) && cleanedArgument.endsWith(DOUBLE_QUOTE)) {
+ cleanedArgument = cleanedArgument.substring(1, cleanedArgument.length() - 1);
}
final StringBuilder buf = new StringBuilder();
diff --git a/src/test/java/org/apache/commons/exec/util/StringUtilTest.java b/src/test/java/org/apache/commons/exec/util/StringUtilTest.java
index cf81fe0e..2f637252 100644
--- a/src/test/java/org/apache/commons/exec/util/StringUtilTest.java
+++ b/src/test/java/org/apache/commons/exec/util/StringUtilTest.java
@@ -20,6 +20,7 @@
package org.apache.commons.exec.util;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;
import java.util.HashMap;
@@ -84,4 +85,18 @@ class StringUtilTest {
assertEquals("This is a FOO & BAR test", StringUtils.stringSubstitution("This is a FOO & BAR test", vars, true).toString());
}
+
+ @Test
+ void testQuoteArgument() throws Exception {
+ assertEquals("hi", StringUtils.quoteArgument("'hi'"));
+ assertEquals("hi", StringUtils.quoteArgument("\"hi\""));
+
+ // cmd : bash -c "echo 'hi'"
+ assertEquals("\"echo 'hi'\"", StringUtils.quoteArgument("echo 'hi'"));
+ assertEquals("'echo \"hi\"'", StringUtils.quoteArgument("echo \"hi\""));
+
+ assertThrows(IllegalArgumentException.class, () -> StringUtils.quoteArgument("echo \"hi 'world'\""),
+ "Can't handle single and double quotes in same argument");
+ }
+
}