Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shell quote #1772

Merged
merged 3 commits into from
Oct 3, 2015
Merged

Shell quote #1772

merged 3 commits into from
Oct 3, 2015

Conversation

chriscool
Copy link
Contributor

Fix shellquote() implementation and add tests as discussed in PR #1757.
Thanks @ion1!

@jbenet jbenet added the status/in-progress In progress label Sep 30, 2015
@chriscool
Copy link
Contributor Author

Yeah \012 caused problems. And still it looks like I messed up something as the tests don't pass.
I will also try to use "%s" instead of '%s' as you are right it should work and be simpler.

@chriscool
Copy link
Contributor Author

I think I should drop the first patch as instead of fixing things it makes them fail.

@chriscool chriscool force-pushed the shell-quote branch 2 times, most recently from 910d316 to 0a87d1d Compare September 30, 2015 21:29
@chriscool
Copy link
Contributor Author

It looks like shellquote() doesn't work the same way on MacOS and on Linux :-(

License: MIT
Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
License: MIT
Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
@chriscool
Copy link
Contributor Author

The problem comes from sed adding a newline character on Mac OS.

Let's try with:

printf '%s' "$(printf '%s' "$_arg" | sed -e "s/'/'\\\\''/g; s/^/'/; s/\$/'/;")"

as encapsulating everything inside a printf should remove the extra newline.

@chriscool
Copy link
Contributor Author

The Travis test failure is not related, so it looks like encapsulating in a printf is working.

@chriscool
Copy link
Contributor Author

@ion1 By the way the Git implementation quotes both ' and ! specially:

https://github.com/git/git/blob/master/quote.c#L23

so maybe we will have to do something for ! too.

@ion1
Copy link

ion1 commented Oct 3, 2015

Huh, interesting. I have never seen a shell which needs special handling for ! within ''s. I tested this with some shells some time ago (link below). The only bytes the shells I tested had problems with were 0 (excluded from the test), newline, 128 and 255 (decimal). I wish they documented their reason for the ! thing.

https://gist.github.com/ion1/7405148

Testing whether you can safely sh-quote any byte string (consisting of no null bytes) by substituting ' with ''' and adding ' on either side. Works in bash, dash, mksh, posh, zsh. Fails in csh, ksh, lksh, pdksh. I was informed OpenBSD ksh works correctly.

@chriscool
Copy link
Contributor Author

It is because of csh-based shells according to the following commit:

git/git@77d604c

@ion1
Copy link

ion1 commented Oct 3, 2015

This should substitute ! with '\!' and ' with '\''.

sed -e 's/\([!'\'']\)/'\''\\\1'\''/g'

@ion1
Copy link

ion1 commented Oct 3, 2015

Since you’re working around the newline issue with a wrapping printf, I wonder if you could just do printf "'%s'" in the wrapper and get the s/^/'/; s/$/'/ out of the sed expression? I understand we have given up on handling newlines given that some shells have trouble with them, but even trying to split a single parameter into two at newlines (which the s///s result in) is still wrong.

Let's first add a comment to explain why the wrapper printf()
is needed.

Then let's replace the last instructions by quotes inside the
wrapper printf() first argument, and let's also put there the
eventual space so that we can remove the printf on the above
line.

License: MIT
Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
@chriscool
Copy link
Contributor Author

Yeah, it is a good idea I think to do printf "'%s'" in the wrapper and get the s/^/'/; s/$/'/ out of the sed expression. The last commit I sent does that and also puts the space in the wrapper printf().

@chriscool
Copy link
Contributor Author

The CI test failure is not related.

jbenet added a commit that referenced this pull request Oct 3, 2015
@jbenet jbenet merged commit 979bcc8 into master Oct 3, 2015
@jbenet jbenet removed the status/in-progress In progress label Oct 3, 2015
@jbenet jbenet deleted the shell-quote branch October 3, 2015 23:34
@jbenet
Copy link
Member

jbenet commented Oct 3, 2015

thanks @chriscool

@chriscool chriscool mentioned this pull request Oct 5, 2015
88 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants