Bug 58898 - ANT wrapper script can not handle double-quote or backslash characters in arguments
Summary: ANT wrapper script can not handle double-quote or backslash characters in arg...
Status: RESOLVED FIXED
Alias: None
Product: Ant
Classification: Unclassified
Component: Wrapper scripts (show other bugs)
Version: unspecified
Hardware: PC Linux
: P2 normal (vote)
Target Milestone: 1.9.7
Assignee: Ant Notifications List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-01-20 21:13 UTC by jwadamson
Modified: 2016-04-22 14:24 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description jwadamson 2016-01-20 21:13:48 UTC
Passing arguments containing double-quote or backslash characters results in errors in ant shell script.

Examples:

$ ant '-Dx="'
/home/jwa/.sdkman/candidates/ant/current/bin/ant: 1: eval: Syntax error: Unterminated quoted string

$ ant "-Dx=\\"
/home/jwa/.sdkman/candidates/ant/current/bin/ant: 1: eval: Syntax error: Unterminated quoted string
Comment 1 Antoine Levy-Lambert 2016-02-22 03:46:11 UTC
Thanks for the patch.
Comment 2 KM 2016-04-19 12:03:33 UTC
As per the WHATSNEW file I see that the modifications for this issue change the interpretation/evaluation of quotes within an ant argument. I have an issue with 1.9.7.  Previously with ant 1.9.6 and earlier the line expanded to -Dbr.cmd="co-r ", which is correct.   Now after updating to Ant 1.9.7 the expansion is messed up.  It actually evaluates to '-Dbr.cmd="co'-r ' " '  including all of the quotes shown.  It’s hard to read so I added spaces around the one double quote at the end.  It actually looks like ‘”’.  Then ant gives the error -r option invalid, because as you can see the -r is abandoned between the literal single quotes.

Needless to say, that I can modify the scripts we use, so the field itself does not contain the double quotes, and that the field is only surrounded by them.  But I was surprised that my build failed almost immediately as ant was kicked off.
Comment 3 jwadamson 2016-04-19 16:18:31 UTC
What is the pertinent arguments and what is the output of execdebug when you run it? e.g.

apache-ant-1.9.7$ ./bin/ant --execdebug -Dbr.cmd="co-r " -version
exec "$JAVACMD" -Xmx512m -classpath "$LOCALCLASSPATH" -Dant.home="$ANT_HOME" -Dant.library.dir="$ANT_LIB" org.apache.tools.ant.launch.Launcher -cp "$CLASSPATH" "-Dbr.cmd=co-r " "-version"
Apache Ant(TM) version 1.9.7 compiled on April 9 2016
Comment 4 KM 2016-04-21 15:52:22 UTC
(In reply to jwadamson from comment #3)
> What is the pertinent arguments and what is the output of execdebug when you
> run it? e.g.
> 
> apache-ant-1.9.7$ ./bin/ant --execdebug -Dbr.cmd="co-r " -version
> exec "$JAVACMD" -Xmx512m -classpath "$LOCALCLASSPATH" -Dant.home="$ANT_HOME"
> -Dant.library.dir="$ANT_LIB" org.apache.tools.ant.launch.Launcher -cp
> "$CLASSPATH" "-Dbr.cmd=co-r " "-version"
> Apache Ant(TM) version 1.9.7 compiled on April 9 2016

I tried to first see how the line gets interpreted and then run the above commands as you suggested.  here is the output:

# with 1.9.6 you get

$ ant --execdebug '-Dbranch.command="co' -r '"' -version
exec "/home/build/softwares/jdk/jre/bin/java" -classpath "/home/build/softwares/ant/lib/ant-launcher.jar" -Dant.home="/home/build/softwares/ant" -Dant.library.dir="/home/build/softwares/ant/lib" org.apache.tools.ant.launch.Launcher -cp "/home/build/softwares/ant/lib/ant.jar:/u04/tomcat/common/lib/servlet.jar" "-Dbranch.command="co" "-r" """ "-version"
Apache Ant(TM) version 1.9.6 compiled on June 29 2015

#with 1.9.7 you get
$ ant --execdebug '-Dbranch.command="co' -r '"' -version
exec "$JAVACMD" -classpath "$LOCALCLASSPATH" -Dant.home="$ANT_HOME" -Dant.library.dir="$ANT_LIB" org.apache.tools.ant.launch.Launcher -cp "$CLASSPATH" "-Dbranch.command=\"co" "-r" "\"" "-version"
Unknown argument: -r

As I've also said, I have no issue reworking the script so the quotes are not part of the value.  That is strange to me anyway.  there is no need to have it that way.
Comment 5 jwadamson 2016-04-21 21:18:22 UTC
Thank you for the example. I think the 1.9.6 behavior in your case was objectively incorrect. It worked out to the right answer, but for the wrong reasons.

'-Dbranch.command="co' -r '"'

In /bin/sh, quoting does not nest. So the example command line has 3 separate arguments passed to ant:
  `-Dbranch.commmand="co`
  `-r`
  `"` (a literal double-quote)

The ant script in 1.9.6 was smushing these back together as (quoting the space between each original argument)
  `"-Dbranch.command="co" "-r" """`. 
/bin/sh processing then interprets this mess as a single argument in the exec command (with some spurious empty strings concatenated into the value):
  `-Dbranch.command=co -r `

The ant script in 1.9.7 keeps those original 3 arguments separate and causes the error about the `-r` argument.
Comment 6 KM 2016-04-22 14:24:51 UTC
(In reply to jwadamson from comment #5)
> Thank you for the example. I think the 1.9.6 behavior in your case was
> objectively incorrect. It worked out to the right answer, but for the wrong
> reasons.
> 
> '-Dbranch.command="co' -r '"'
> 
> In /bin/sh, quoting does not nest. So the example command line has 3
> separate arguments passed to ant:
>   `-Dbranch.commmand="co`
>   `-r`
>   `"` (a literal double-quote)
> 
> The ant script in 1.9.6 was smushing these back together as (quoting the
> space between each original argument)
>   `"-Dbranch.command="co" "-r" """`. 
> /bin/sh processing then interprets this mess as a single argument in the
> exec command (with some spurious empty strings concatenated into the value):
>   `-Dbranch.command=co -r `
> 
> The ant script in 1.9.7 keeps those original 3 arguments separate and causes
> the error about the `-r` argument.

I agree. Once I looked at the evaluated value, I was actually surprised it worked.  I've reworked the scripts to pass it the correct way.  Only the value, and no double quotes.  works fine in both versions of ant.