Skip to contentSkip to conversation

here documents, command substitution, and the { keyword

BUG? Jonathan Nieder 2 participants 3 messages Nov 7, 2010 – Mar 15, 2011

lore
Share & tools
lore
#0 lore raw

Hi,

Just ran into some strange behavior:

$ cat test.sh
#!/bin/sh
echo hello >greeting
cat <<EOF &&
$(cat greeting)
EOF
{
	echo $?
	cat greeting
} >/dev/null
$ sh test.sh
hello
test.sh: 7: {: not found
127
hello
test.sh: 10: Syntax error: "}" unexpected

bash, mksh, pdksh, and ksh93 all print hello as expected. The problem is reproducible with all versions of dash in the git repo.

Ideas?
Jonathan

#1 lore raw
Quoted 9 lines · Jonathan Nieder
$ sh test.sh
hello
test.sh: 7: {: not found
127
hello
test.sh: 10: Syntax error: "}" unexpected

bash, mksh, pdksh, and ksh93 all print hello as expected. The problem is reproducible with all versions of dash in the git repo.

err, by this I mean:

 $ bash test.sh
 hello
 $

and that the strange dash behavior is reproducible with 016b529 and 05c1076ba2 (I didn't try the revisions in between). Sorry for the noise.

#2 lore raw
In reply to Jonathan Nieder
Quoted 2 lines · Jonathan Nieder

Just ran into some strange behavior:

This patch should fix the problem.

commit ec2c84d3c4dba4b74440d72bdd1de416a9acd2a9

Author: Herbert Xu <[email protected]>
Date:   Tue Mar 15 17:41:53 2011 +0800

[PARSER] Fix clobbering of checkkwd

On Sun, Nov 07, 2010 at 02:21:21AM +0000, Jonathan Nieder wrote:
>
> Just ran into some strange behavior:
>
> $ cat test.sh
> #!/bin/sh
> echo hello >greeting
> cat <<EOF &&
> $(cat greeting)
> EOF
> {
> echo $?
> cat greeting
> } >/dev/null
>
>
> $ sh test.sh
> hello
> test.sh: 7: {: not found
> 127
> hello
> test.sh: 10: Syntax error: "}" unexpected
>
> bash, mksh, pdksh, and ksh93 all print hello as expected. The problem > is reproducible with all versions of dash in the git repo.

This is caused by the clobbering of checkkwd due to readtoken recursion while parsing a here document.

This patch fixes it by saving the original value of checkkwd.

ChangeLog +4 -0
index 5b6d53e..50a8044 100644
@@ -1,3 +1,7 @@
+2011-03-15  Herbert Xu <[email protected]>
+
+	* Fix clobbering of checkkwd.
+
 2011-03-15  Jonathan Nieder <[email protected]>
 
 	* Free IFS state after here document expansion.
src/parser.c +3 -2
index 0bfd620..528d005 100644
@@ -687,6 +687,7 @@ STATIC int
 readtoken(void)
 {
 	int t;
+	int kwd = checkkwd;
 #ifdef DEBUG
 	int alreadyseen = tokpushback;
 #endif
@@ -697,7 +698,7 @@ top:
 	/*
 	 * eat newlines
 	 */
-	if (checkkwd & CHKNL) {
+	if (kwd & CHKNL) {
 		while (t == TNL) {
 			parseheredoc();
 			t = xxreadtoken();
@@ -711,7 +712,7 @@ top:
 	/*
 	 * check for keywords
 	 */
-	if (checkkwd & CHKKWD) {
+	if (kwd & CHKKWD) {
 		const char *const *pp;
 
 		if ((pp = findkwd(wordtext))) {

Cheers,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt