[ 
https://issues.apache.org/jira/browse/MSHARED-750?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16561075#comment-16561075
 ] 

ASF GitHub Bot commented on MSHARED-750:
----------------------------------------

khmarbaise closed pull request #4: [MSHARED-750] - Unbalanced quotes in command 
with escaped double quot…
URL: https://github.com/apache/maven-shared-utils/pull/4
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/src/main/java/org/apache/maven/shared/utils/cli/CommandLineUtils.java 
b/src/main/java/org/apache/maven/shared/utils/cli/CommandLineUtils.java
index 4beea13..3a745ab 100644
--- a/src/main/java/org/apache/maven/shared/utils/cli/CommandLineUtils.java
+++ b/src/main/java/org/apache/maven/shared/utils/cli/CommandLineUtils.java
@@ -477,8 +477,9 @@ private static boolean isAlive( Process p )
         final int normal = 0;
         final int inQuote = 1;
         final int inDoubleQuote = 2;
+        boolean inEscape = false;
         int state = normal;
-        StringTokenizer tok = new StringTokenizer( toProcess, "\"\' ", true );
+        final StringTokenizer tok = new StringTokenizer( toProcess, "\"\' \\", 
true );
         List<String> tokens = new ArrayList<String>();
         StringBuilder current = new StringBuilder();
 
@@ -490,31 +491,65 @@ private static boolean isAlive( Process p )
                 case inQuote:
                     if ( "\'".equals( nextTok ) )
                     {
-                        state = normal;
+                        if ( inEscape )
+                        {
+                            current.append( nextTok );
+                            inEscape = false;
+                        }
+                        else
+                        {
+                            state = normal;
+                        }
                     }
                     else
                     {
                         current.append( nextTok );
+                        inEscape = "\\".equals( nextTok );
                     }
                     break;
                 case inDoubleQuote:
                     if ( "\"".equals( nextTok ) )
                     {
-                        state = normal;
+                        if ( inEscape )
+                        {
+                            current.append( nextTok );
+                            inEscape = false;
+                        }
+                        else
+                        {
+                            state = normal;
+                        }
                     }
                     else
                     {
                         current.append( nextTok );
+                        inEscape = "\\".equals( nextTok );
                     }
                     break;
                 default:
                     if ( "\'".equals( nextTok ) )
                     {
-                        state = inQuote;
+                        if ( inEscape )
+                        {
+                            inEscape = false;
+                            current.append( nextTok );
+                        }
+                        else
+                        {
+                            state = inQuote;
+                        }
                     }
                     else if ( "\"".equals( nextTok ) )
                     {
-                        state = inDoubleQuote;
+                        if ( inEscape )
+                        {
+                            inEscape = false;
+                            current.append( nextTok );
+                        }
+                        else
+                            {
+                            state = inDoubleQuote;
+                        }
                     }
                     else if ( " ".equals( nextTok ) )
                     {
@@ -527,6 +562,7 @@ else if ( " ".equals( nextTok ) )
                     else
                     {
                         current.append( nextTok );
+                        inEscape = "\\".equals( nextTok );
                     }
                     break;
             }
diff --git 
a/src/test/java/org/apache/maven/shared/utils/cli/CommandLineUtilsTest.java 
b/src/test/java/org/apache/maven/shared/utils/cli/CommandLineUtilsTest.java
index 4ed35ed..d64fdd8 100644
--- a/src/test/java/org/apache/maven/shared/utils/cli/CommandLineUtilsTest.java
+++ b/src/test/java/org/apache/maven/shared/utils/cli/CommandLineUtilsTest.java
@@ -22,6 +22,7 @@
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.fail;
 
 import java.util.Arrays;
 import java.util.HashMap;
@@ -99,27 +100,38 @@ public void testTranslateCommandline()
         assertCmdLineArgs( new String[] { "foo", " ' ", "bar" }, "foo \" ' \" 
bar" );
     }
 
-    @Test
-    public void 
givenASingleQuoteMarkInArgument_whenExecutingCode_thenExitCode0Returned() 
throws Exception {
-        final Process p = exec("echo \"let's go\"");
 
-        assertEquals(0, p.exitValue());
+    @Test
+    public void 
givenASingleQuoteMarkInArgument_whenTranslatingToCmdLineArgs_thenTheQuotationMarkIsNotEscaped()
 throws Exception
+    {
+        final String command = "echo \"let's go\"";
+        final String[] expected = new String[]{"echo", "let's go"};
+        assertCmdLineArgs(expected, command);
     }
 
     @Test
-    public void 
givenADoubleQuoteMarkInArgument_whenExecutingCode_thenExitCode0Returned() 
throws Exception {
-        final Process p = exec("echo \"let\"s go\"");
+    public void 
givenAnEscapedDoubleQuoteMarkInArgument_whenTranslatingToCmdLineArgs_thenTheQuotationMarkRemainsEscaped()
 throws Exception
+    {
+        final String command = "echo \"let\\\"s go\"";
+        final String[] expected = new String[]{"echo", "let\\\"s go"};
+        assertCmdLineArgs(expected, command);
+    }
 
-        assertEquals(0, p.exitValue());
+    @Test
+    public void 
givenAnEscapedSingleQuoteMarkInArgument_whenTranslatingToCmdLineArgs_thenTheQuotationMarkRemainsEscaped()
 throws Exception
+    {
+        final String command = "echo \"let\\\'s go\"";
+        final String[] expected = new String[]{"echo", "let\\\'s go"};
+        assertCmdLineArgs(expected, command);
     }
 
-    private Process exec(String cmd) throws CommandLineException, 
InterruptedException {
-        Process p = new Commandline(cmd).execute();
-        Thread.sleep(1000);
-        return p;
+    @Test
+    public void 
givenAnEscapedDoubleQuoteMarkInArgument_whenTranslatingToCmdLineArgs_thenNoExceptionIsThrown()
 throws Exception
+    {
+        new Commandline("echo \"let\\\"s go\"").execute();
     }
 
-    private void assertCmdLineArgs( String[] expected, String cmdLine )
+    private void assertCmdLineArgs( final String[] expected, final String 
cmdLine )
         throws Exception
     {
         String[] actual = CommandLineUtils.translateCommandline( cmdLine );


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> Unbalanced quotes in command with escaped double quotation mark
> ---------------------------------------------------------------
>
>                 Key: MSHARED-750
>                 URL: https://issues.apache.org/jira/browse/MSHARED-750
>             Project: Maven Shared Components
>          Issue Type: Bug
>    Affects Versions: maven-shared-utils-3.2.1
>            Reporter: Kathryn Newbould
>            Assignee: Karl Heinz Marbaise
>            Priority: Major
>             Fix For: maven-shared-utils-3.3.0
>
>
> Due to MSHARED-749, the following test will error (due to an exception being 
> thrown) in master maven:
> {code:java}
> @Test
> public void 
> givenAnEscapedDoubleQuoteMarkInArgument_whenPreparingToExecuteCode_thenTheQuotationMarkRemainsEscaped()
>  throws Exception {
> final String command = "echo \"let\\\"s go\"";
> final String[] actual = CommandLineUtils.translateCommandline(command);
> final String[] expected = new String[]{"echo", "let\\\"s go"};
> assertArrayEquals(expected, actual);
> }
> {code}
> Results:
> {code:java}
> org.apache.maven.shared.utils.cli.CommandLineException: unbalanced quotes in 
> echo "let\"s go"{code}
>  



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to