Skip to contentSkip to conversation

hush: Fix assignments being misinterpreted as commands

PATCH New Sean Anderson 4 participants 13 messages Feb 28, 2021 – Apr 13, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hush: Fix assignments being misinterpreted as commands

If there were no variable substitutions in a command, then initial assignments would be misinterpreted as commands, instead of being skipped over. This is demonstrated by the following example:

=> foo=bar echo baz
Unknown command 'foo=bar' - try 'help'

---

 common/cli_hush.c    | 2 +-
 test/cmd/test_echo.c | 2 ++
 2 files changed, 3 insertions(+), 1 deletion(-)
common/cli_hush.c +1 -1
index b7f0f0ff41..1b9bef64b6 100644
@@ -1672,7 +1672,7 @@ static int run_pipe_real(struct pipe *pi)
 			return -1;
 		}
 		/* Process the command */
-		return cmd_process(flag, child->argc, child->argv,
+		return cmd_process(flag, child->argc - i, child->argv + i,
 				   &flag_repeat, NULL);
 #endif
 	}
test/cmd/test_echo.c +2 -0
index 4183cf75bb..13e1fb7c82 100644
@@ -33,6 +33,8 @@ static struct test_data echo_data[] = {
 	 */
 	{"setenv jQx X; echo \"a)\" ${jQx} 'b)' '${jQx}' c) ${jQx}; setenv jQx",
 	 "a) X b) ${jQx} c) X"},
+	/* Test shell variable assignments without substitutions */
+	{"foo=bar echo baz", "baz"},
 	/* Test handling of shell variables. */
 	{"setenv jQx; for jQx in 1 2 3; do echo -n \"${jQx}, \"; done; echo;",
 	 "1, 2, 3, "},
Signature · 1 line
2.30.1
#1 lore raw

PATCH 1/1 hush: Fix assignments being misinterpreted as commands

Am 28. Februar 2021 22:29:51 MEZ schrieb Sean Anderson <[email protected]>:

Quoted 6 lines

If there were no variable substitutions in a command, then initial assignments would be misinterpreted as commands, instead of being skipped
over. This is demonstrated by the following example:

=> foo=bar echo baz

The commit message does not explain why this patch is needed.

What shall be the value off foo after this line?

What will be the output of

foo=bar echo ${foo}

with and without yor patch?

Best regards

Heinrich

Quoted 36 lines

Unknown command 'foo=bar' - try 'help'

---

common/cli_hush.c | 2 +-
test/cmd/test_echo.c | 2 ++

2 files changed, 3 insertions(+), 1 deletion(-)
common/cli_hush.c +1 -1
index b7f0f0ff41..1b9bef64b6 100644
@@ -1672,7 +1672,7 @@ static int run_pipe_real(struct pipe *pi)
 			return -1;
 		}
 		/* Process the command */
-		return cmd_process(flag, child->argc, child->argv,
+		return cmd_process(flag, child->argc - i, child->argv + i,
 				   &flag_repeat, NULL);
 #endif
 	}
test/cmd/test_echo.c +2 -0
index 4183cf75bb..13e1fb7c82 100644
@@ -33,6 +33,8 @@ static struct test_data echo_data[] = {
 	 */
 	{"setenv jQx X; echo \"a)\" ${jQx} 'b)' '${jQx}' c) ${jQx}; setenv
 jQx",
 	 "a) X b) ${jQx} c) X"},
+	/* Test shell variable assignments without substitutions */
+	{"foo=bar echo baz", "baz"},
 	/* Test handling of shell variables. */
 	{"setenv jQx; for jQx in 1 2 3; do echo -n \"${jQx}, \"; done; echo;",
	 "1, 2, 3, "},
#2 lore raw

PATCH 1/1 hush: Fix assignments being misinterpreted as commands

Quoted 9 lines · Heinrich Schuchardt

Am 28. Februar 2021 22:29:51 MEZ schrieb Sean Anderson <[email protected]>:

Quoted 6 lines

If there were no variable substitutions in a command, then initial assignments would be misinterpreted as commands, instead of being skipped
over. This is demonstrated by the following example:

=> foo=bar echo baz

The commit message does not explain why this patch is needed.

This is a bug I noticed while writing some tests of hush.

Quoted 1 line

What shall be the value off foo after this line?

It should be bar. This is an existing difference when compared with bash. For example, without this patch, we have

=> foo=bar echo $foo
bar
=> echo $foo
bar

Quoted 6 lines

What will be the output of

foo=bar echo ${foo}

with and without yor patch?

It is the same.

--Sean

Quoted 43 lines

Best regards

Heinrich

Quoted 36 lines

Unknown command 'foo=bar' - try 'help'

---

common/cli_hush.c | 2 +-
test/cmd/test_echo.c | 2 ++

2 files changed, 3 insertions(+), 1 deletion(-)
common/cli_hush.c +1 -1
index b7f0f0ff41..1b9bef64b6 100644
@@ -1672,7 +1672,7 @@ static int run_pipe_real(struct pipe *pi)
 			return -1;
 		}
 		/* Process the command */
-		return cmd_process(flag, child->argc, child->argv,
+		return cmd_process(flag, child->argc - i, child->argv + i,
 				   &flag_repeat, NULL);
 #endif
 	}
test/cmd/test_echo.c +2 -0
index 4183cf75bb..13e1fb7c82 100644
@@ -33,6 +33,8 @@ static struct test_data echo_data[] = {
 	 */
 	{"setenv jQx X; echo \"a)\" ${jQx} 'b)' '${jQx}' c) ${jQx}; setenv
 jQx",
 	 "a) X b) ${jQx} c) X"},
+	/* Test shell variable assignments without substitutions */
+	{"foo=bar echo baz", "baz"},
 	/* Test handling of shell variables. */
 	{"setenv jQx; for jQx in 1 2 3; do echo -n \"${jQx}, \"; done; echo;",
	 "1, 2, 3, "},
#3 lore raw

PATCH 1/1 hush: Fix assignments being misinterpreted as commands

Quoted 24 lines · Sean Anderson
Quoted 9 lines · Heinrich Schuchardt

Am 28. Februar 2021 22:29:51 MEZ schrieb Sean Anderson <[email protected]>:

Quoted 6 lines

If there were no variable substitutions in a command, then initial assignments would be misinterpreted as commands, instead of being skipped
over. This is demonstrated by the following example:

=> foo=bar echo baz

The commit message does not explain why this patch is needed.

This is a bug I noticed while writing some tests of hush.

Quoted 1 line

What shall be the value off foo after this line?

It should be bar. This is an existing difference when compared with bash. For example, without this patch, we have

=> foo=bar echo $foo
bar
=> echo $foo
bar

Quoted 1 line
Quoted 7 lines
Quoted 5 lines

What will be the output of

foo=bar echo ${foo}

with and without yor patch?

It is the same.

bash works as you describe. dash and busybox-sh both function like this:

$ foo=bar echo $foo
$ echo $foo

$

That we error out entirely is different from everyone. Is that a good thing? Maybe. I know I've caught myself making thinkos due to that logic. It does also violate the principal of least surprise, that we don't act like anything else. But I would suggest the behavior of busybox-sh (what we forked long long ago) is what we should model here rather than be more bash-like. I'm not all that firm on this opinion frankly, especially given the one-line nature of the change to bring us that behavior and I assume dash/busybox are acting like pure sh would in this case, which we aren't anyhow.

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20210301/c126f507/attachment.sig>
#4 lore raw

PATCH 1/1 hush: Fix assignments being misinterpreted as commands

Quoted 16 lines · Tom Rini
Quoted 15 lines · Sean Anderson
Quoted 9 lines · Heinrich Schuchardt

Am 28. Februar 2021 22:29:51 MEZ schrieb Sean Anderson <[email protected]>:

Quoted 6 lines

If there were no variable substitutions in a command, then initial assignments would be misinterpreted as commands, instead of being skipped
over. This is demonstrated by the following example:

=> foo=bar echo baz

The commit message does not explain why this patch is needed.

This is a bug I noticed while writing some tests of hush.

Quoted 1 line

What shall be the value off foo after this line?

Quoted 7 lines
Quoted 7 lines

It should be bar. This is an existing difference when compared with bash. For example, without this patch, we have

=> foo=bar echo $foo
bar
=> echo $foo
bar

This seems really awkward. In bash I get:

$ foo=bar ./test.sh
bar
$ echo $foo

$

Where test.sh

#!/bin/sh
echo $foo

I did not expect an assignment made before a command to stick.

Quoted 9 lines
Quoted 9 lines
Quoted 7 lines

What will be the output of

foo=bar echo ${foo}

with and without your patch?

It is the same.

Please, provide an example where the patch makes a difference.

Best regards

Heinrich

Quoted 19 lines

bash works as you describe. dash and busybox-sh both function like this:

$ foo=bar echo $foo
$ echo $foo

$

That we error out entirely is different from everyone. Is that a good thing? Maybe. I know I've caught myself making thinkos due to that logic. It does also violate the principal of least surprise, that we don't act like anything else. But I would suggest the behavior of busybox-sh (what we forked long long ago) is what we should model here rather than be more bash-like. I'm not all that firm on this opinion frankly, especially given the one-line nature of the change to bring us that behavior and I assume dash/busybox are acting like pure sh would in this case, which we aren't anyhow.

#5 lore raw

PATCH 1/1 hush: Fix assignments being misinterpreted as commands

Quoted 31 lines · Heinrich Schuchardt
Quoted 16 lines · Tom Rini
Quoted 15 lines · Sean Anderson
Quoted 9 lines · Heinrich Schuchardt

Am 28. Februar 2021 22:29:51 MEZ schrieb Sean Anderson <[email protected]>:

Quoted 6 lines

If there were no variable substitutions in a command, then initial assignments would be misinterpreted as commands, instead of being skipped
over. This is demonstrated by the following example:

=> foo=bar echo baz

The commit message does not explain why this patch is needed.

This is a bug I noticed while writing some tests of hush.

Quoted 1 line

What shall be the value off foo after this line?

Quoted 7 lines
Quoted 7 lines

It should be bar. This is an existing difference when compared with bash. For example, without this patch, we have

=> foo=bar echo $foo
bar
=> echo $foo
bar

This seems really awkward. In bash I get:

$ foo=bar ./test.sh
bar
$ echo $foo
Quoted 8 lines

$

Where test.sh

#!/bin/sh
echo $foo

I did not expect an assignment made before a command to stick.

Yeah, this is because hush does not have the concept of per-command assignments (scope). So everything happens in the global scope.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

What will be the output of

foo=bar echo ${foo}

with and without your patch?

Quoted 26 lines
Quoted 1 line
Quoted 1 line

It is the same.

Please, provide an example where the patch makes a difference.

Best regards

Heinrich

Quoted 18 lines

bash works as you describe. dash and busybox-sh both function like this:

$ foo=bar echo $foo
$ echo $foo

$

That we error out entirely is different from everyone. Is that a good thing? Maybe. I know I've caught myself making thinkos due to that logic. It does also violate the principal of least surprise, that we don't act like anything else. But I would suggest the behavior of busybox-sh (what we forked long long ago) is what we should model here rather than be more bash-like. I'm not all that firm on this opinion frankly, especially given the one-line nature of the change to bring us that behavior and I assume dash/busybox are acting like pure sh would in this case, which we aren't anyhow.

Ok, I'd like to clear things up. Here is the current behavior of U-Boot:

=> foo=bar echo $foo
bar
=> echo $foo
bar
=> baz=bar echo qux
Unknown command 'baz=bar' - try 'help'

with this patch, this changes to

=> foo=bar echo $foo
bar
=> echo $foo
bar
=> baz=bar echo qux
qux

This patch *only* affects cases where there is an assignment at the beginning of the line, but there is *no* variable reference in the command. I know this is an edge case, but the current logic is clearly wrong here.

--Sean

#6 lore raw

PATCH 1/1 hush: Fix assignments being misinterpreted as commands

Quoted 82 lines · Sean Anderson
Quoted 31 lines · Heinrich Schuchardt
Quoted 16 lines · Tom Rini
Quoted 15 lines · Sean Anderson
Quoted 9 lines · Heinrich Schuchardt

Am 28. Februar 2021 22:29:51 MEZ schrieb Sean Anderson <[email protected]>:

Quoted 6 lines

If there were no variable substitutions in a command, then initial assignments would be misinterpreted as commands, instead of being skipped
over. This is demonstrated by the following example:

=> foo=bar echo baz

The commit message does not explain why this patch is needed.

This is a bug I noticed while writing some tests of hush.

Quoted 1 line

What shall be the value off foo after this line?

Quoted 7 lines
Quoted 7 lines

It should be bar. This is an existing difference when compared with bash. For example, without this patch, we have

=> foo=bar echo $foo
bar
=> echo $foo
bar

This seems really awkward. In bash I get:

$ foo=bar ./test.sh
bar
$ echo $foo
Quoted 8 lines

$

Where test.sh

#!/bin/sh
echo $foo

I did not expect an assignment made before a command to stick.

Yeah, this is because hush does not have the concept of per-command assignments (scope). So everything happens in the global scope.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

What will be the output of

foo=bar echo ${foo}

with and without your patch?

Quoted 26 lines
Quoted 1 line
Quoted 1 line

It is the same.

Please, provide an example where the patch makes a difference.

Best regards

Heinrich

Quoted 18 lines

bash works as you describe. dash and busybox-sh both function like this:

$ foo=bar echo $foo
$ echo $foo

$

That we error out entirely is different from everyone. Is that a good thing? Maybe. I know I've caught myself making thinkos due to that logic. It does also violate the principal of least surprise, that we don't act like anything else. But I would suggest the behavior of busybox-sh (what we forked long long ago) is what we should model here rather than be more bash-like. I'm not all that firm on this opinion frankly, especially given the one-line nature of the change to bring us that behavior and I assume dash/busybox are acting like pure sh would in this case, which we aren't anyhow.

Ok, I'd like to clear things up. Here is the current behavior of U-Boot:

Quoted 6 lines

=> foo=bar echo $foo
bar
=> echo $foo
bar
=> baz=bar echo qux
Unknown command 'baz=bar' - try 'help'

I see this as well (which isn't what I said in another part of this thread, so I re-checked just now).

Quoted 9 lines

with this patch, this changes to

=> foo=bar echo $foo
bar
=> echo $foo
bar
=> baz=bar echo qux
qux

Quoted 4 lines

This patch *only* affects cases where there is an assignment at the beginning of the line, but there is *no* variable reference in the command. I know this is an edge case, but the current logic is clearly wrong here.

OK. But I don't see that behavior in bash 4.4.20 (Ubuntu 18.04). Where does one get a shell that works like you're changing our hush to?

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20210302/c9fc4641/attachment.sig>
#7 lore raw

PATCH 1/1 hush: Fix assignments being misinterpreted as commands

Quoted 102 lines · Tom Rini
Quoted 82 lines · Sean Anderson
Quoted 31 lines · Heinrich Schuchardt
Quoted 16 lines · Tom Rini
Quoted 15 lines · Sean Anderson
Quoted 9 lines · Heinrich Schuchardt

Am 28. Februar 2021 22:29:51 MEZ schrieb Sean Anderson <[email protected]>:

Quoted 6 lines

If there were no variable substitutions in a command, then initial assignments would be misinterpreted as commands, instead of being skipped
over. This is demonstrated by the following example:

=> foo=bar echo baz

The commit message does not explain why this patch is needed.

This is a bug I noticed while writing some tests of hush.

Quoted 1 line

What shall be the value off foo after this line?

Quoted 7 lines
Quoted 7 lines

It should be bar. This is an existing difference when compared with bash. For example, without this patch, we have

=> foo=bar echo $foo
bar
=> echo $foo
bar

This seems really awkward. In bash I get:

$ foo=bar ./test.sh
bar
$ echo $foo
Quoted 8 lines

$

Where test.sh

#!/bin/sh
echo $foo

I did not expect an assignment made before a command to stick.

Yeah, this is because hush does not have the concept of per-command assignments (scope). So everything happens in the global scope.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

What will be the output of

foo=bar echo ${foo}

with and without your patch?

Quoted 26 lines
Quoted 1 line
Quoted 1 line

It is the same.

Please, provide an example where the patch makes a difference.

Best regards

Heinrich

Quoted 18 lines

bash works as you describe. dash and busybox-sh both function like this:

$ foo=bar echo $foo
$ echo $foo

$

That we error out entirely is different from everyone. Is that a good thing? Maybe. I know I've caught myself making thinkos due to that logic. It does also violate the principal of least surprise, that we don't act like anything else. But I would suggest the behavior of busybox-sh (what we forked long long ago) is what we should model here rather than be more bash-like. I'm not all that firm on this opinion frankly, especially given the one-line nature of the change to bring us that behavior and I assume dash/busybox are acting like pure sh would in this case, which we aren't anyhow.

Ok, I'd like to clear things up. Here is the current behavior of U-Boot:

Quoted 6 lines

=> foo=bar echo $foo
bar
=> echo $foo
bar
=> baz=bar echo qux
Unknown command 'baz=bar' - try 'help'

I see this as well (which isn't what I said in another part of this thread, so I re-checked just now).

Quoted 9 lines

with this patch, this changes to

=> foo=bar echo $foo
bar
=> echo $foo
bar
=> baz=bar echo qux
qux

Quoted 8 lines
Quoted 4 lines

This patch *only* affects cases where there is an assignment at the beginning of the line, but there is *no* variable reference in the command. I know this is an edge case, but the current logic is clearly wrong here.

OK. But I don't see that behavior in bash 4.4.20 (Ubuntu 18.04). Where does one get a shell that works like you're changing our hush to?

I'm not changing hush to work like the first two examples, it's already like this.

Only the third example is affected by this patch.

--Sean

#8 lore raw

PATCH 1/1 hush: Fix assignments being misinterpreted as commands

Quoted 108 lines · Sean Anderson
Quoted 107 lines · Tom Rini
Quoted 82 lines · Sean Anderson
Quoted 31 lines · Heinrich Schuchardt
Quoted 16 lines · Tom Rini
Quoted 15 lines · Sean Anderson
Quoted 9 lines · Heinrich Schuchardt

Am 28. Februar 2021 22:29:51 MEZ schrieb Sean Anderson <[email protected]>:

Quoted 6 lines

If there were no variable substitutions in a command, then initial assignments would be misinterpreted as commands, instead of being skipped
over. This is demonstrated by the following example:

=> foo=bar echo baz

The commit message does not explain why this patch is needed.

This is a bug I noticed while writing some tests of hush.

Quoted 1 line

What shall be the value off foo after this line?

Quoted 7 lines
Quoted 7 lines

It should be bar. This is an existing difference when compared with bash. For example, without this patch, we have

=> foo=bar echo $foo
bar
=> echo $foo
bar

This seems really awkward. In bash I get:

$ foo=bar ./test.sh
bar
$ echo $foo
Quoted 8 lines

$

Where test.sh

#!/bin/sh
echo $foo

I did not expect an assignment made before a command to stick.

Yeah, this is because hush does not have the concept of per-command assignments (scope). So everything happens in the global scope.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

What will be the output of

foo=bar echo ${foo}

with and without your patch?

Quoted 26 lines
Quoted 1 line
Quoted 1 line

It is the same.

Please, provide an example where the patch makes a difference.

Best regards

Heinrich

Quoted 18 lines

bash works as you describe. dash and busybox-sh both function like this:

$ foo=bar echo $foo
$ echo $foo

$

That we error out entirely is different from everyone. Is that a good thing? Maybe. I know I've caught myself making thinkos due to that logic. It does also violate the principal of least surprise, that we don't act like anything else. But I would suggest the behavior of busybox-sh (what we forked long long ago) is what we should model here rather than be more bash-like. I'm not all that firm on this opinion frankly, especially given the one-line nature of the change to bring us that behavior and I assume dash/busybox are acting like pure sh would in this case, which we aren't anyhow.

Ok, I'd like to clear things up. Here is the current behavior of U-Boot:

Quoted 6 lines

=> foo=bar echo $foo
bar
=> echo $foo
bar
=> baz=bar echo qux
Unknown command 'baz=bar' - try 'help'

I see this as well (which isn't what I said in another part of this thread, so I re-checked just now).

Quoted 13 lines

with this patch, this changes to

=> foo=bar echo $foo
bar
=> echo $foo
bar
=> baz=bar echo qux
qux

This patch *only* affects cases where there is an assignment at the beginning of the line, but there is *no* variable reference in the command. I know this is an edge case, but the current logic is clearly wrong here.

Quoted 7 lines
Quoted 3 lines

OK. But I don't see that behavior in bash 4.4.20 (Ubuntu 18.04). Where does one get a shell that works like you're changing our hush to?

I'm not changing hush to work like the first two examples, it's already like this.

Only the third example is affected by this patch.

OK. But to what end? Historically we have a buggy but mostly compatible "hush" that acts like "sh" does. A more flexible shell could solve a lot of different use cases including making boot scripts that people end up writing being clearer and easier to write/debug/maintain. What I worry about here is making our shell not act like any regular shell people use.

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20210302/277c60b0/attachment.sig>
#9 lore raw

PATCH 1/1 hush: Fix assignments being misinterpreted as commands

Quoted 115 lines · Tom Rini
Quoted 114 lines · Sean Anderson
Quoted 102 lines · Tom Rini
Quoted 82 lines · Sean Anderson
Quoted 31 lines · Heinrich Schuchardt
Quoted 16 lines · Tom Rini
Quoted 15 lines · Sean Anderson
Quoted 9 lines · Heinrich Schuchardt

Am 28. Februar 2021 22:29:51 MEZ schrieb Sean Anderson <[email protected]>:

Quoted 6 lines

If there were no variable substitutions in a command, then initial assignments would be misinterpreted as commands, instead of being skipped
over. This is demonstrated by the following example:

=> foo=bar echo baz

The commit message does not explain why this patch is needed.

This is a bug I noticed while writing some tests of hush.

Quoted 1 line

What shall be the value off foo after this line?

Quoted 7 lines
Quoted 7 lines

It should be bar. This is an existing difference when compared with bash. For example, without this patch, we have

=> foo=bar echo $foo
bar
=> echo $foo
bar

This seems really awkward. In bash I get:

$ foo=bar ./test.sh
bar
$ echo $foo
Quoted 8 lines

$

Where test.sh

#!/bin/sh
echo $foo

I did not expect an assignment made before a command to stick.

Yeah, this is because hush does not have the concept of per-command assignments (scope). So everything happens in the global scope.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

What will be the output of

foo=bar echo ${foo}

with and without your patch?

Quoted 26 lines
Quoted 1 line
Quoted 1 line

It is the same.

Please, provide an example where the patch makes a difference.

Best regards

Heinrich

Quoted 18 lines

bash works as you describe. dash and busybox-sh both function like this:

$ foo=bar echo $foo
$ echo $foo

$

That we error out entirely is different from everyone. Is that a good thing? Maybe. I know I've caught myself making thinkos due to that logic. It does also violate the principal of least surprise, that we don't act like anything else. But I would suggest the behavior of busybox-sh (what we forked long long ago) is what we should model here rather than be more bash-like. I'm not all that firm on this opinion frankly, especially given the one-line nature of the change to bring us that behavior and I assume dash/busybox are acting like pure sh would in this case, which we aren't anyhow.

Ok, I'd like to clear things up. Here is the current behavior of U-Boot:

Quoted 6 lines

=> foo=bar echo $foo
bar
=> echo $foo
bar
=> baz=bar echo qux
Unknown command 'baz=bar' - try 'help'

I see this as well (which isn't what I said in another part of this thread, so I re-checked just now).

Quoted 9 lines

with this patch, this changes to

=> foo=bar echo $foo
bar
=> echo $foo
bar
=> baz=bar echo qux
qux

Quoted 8 lines
Quoted 4 lines

This patch *only* affects cases where there is an assignment at the beginning of the line, but there is *no* variable reference in the command. I know this is an edge case, but the current logic is clearly wrong here.

OK. But I don't see that behavior in bash 4.4.20 (Ubuntu 18.04). Where does one get a shell that works like you're changing our hush to?

I'm not changing hush to work like the first two examples, it's already like this.

Quoted 9 lines
Quoted 1 line

Only the third example is affected by this patch.

OK. But to what end? Historically we have a buggy but mostly compatible "hush" that acts like "sh" does. A more flexible shell could solve a lot of different use cases including making boot scripts that people end up writing being clearer and easier to write/debug/maintain. What I worry about here is making our shell not act like any regular shell people use.

Good news: this patch brings hush *more* in-line with other shells.

dash:

	$ foo=bar echo qux
	qux

sh:
sh-5.1$ foo=bar echo qux
qux

bash:

	$ foo=bar echo qux
	qux

U-Boot without this patch:
=> foo=bar echo qux
Unknown command 'foo=bar' - try 'help'

U-Boot with this patch:
=> foo=bar echo qux
qux

--Sean

#10 lore raw

PATCH 1/1 hush: Fix assignments being misinterpreted as commands

In reply to Tom Rini

Dear Tom,

Quoted 9 lines · In message <20210302133435.GZ10169@bill-the-cat> you wrote:
Quoted 2 lines

Only the third example is affected by this patch.

OK. But to what end? Historically we have a buggy but mostly compatible "hush" that acts like "sh" does. A more flexible shell could solve a lot of different use cases including making boot scripts that people end up writing being clearer and easier to write/debug/maintain. What I worry about here is making our shell not act like any regular shell people use.

I am always surprised why people try to add minor fixes (and sometimes bells and whistles) to ancient versions of the software without checking recent code first.

The shell in busybox v1.32 shows this behaviour:

	$ foo=bar echo $foo
	$ echo $foo
	$ baz=bar echo qux
	qux
	$

So the specific problem has (long) been fixed in upstream, and instead of adding a patch to our old version, thus cementing the broken behaviour, we should upgrade hush to recent upstream code.

Yes, I am aware that this is a lot more effort that this little patch, but if we could combine the efforts that have already been wasted over the years for such little fixes here and there wew could have upgraded long ago.

Grooming a dead horse makes little sense to me.

Best regards,

Wolfgang Denk

Signature · 4 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
What is research but a blind date with knowledge?      -- Will Harvey
#11 lore raw

PATCH 1/1 hush: Fix assignments being misinterpreted as commands

In reply to Sean Anderson
Quoted 32 lines · Sean Anderson

If there were no variable substitutions in a command, then initial assignments would be misinterpreted as commands, instead of being skipped over. This is demonstrated by the following example:

=> foo=bar echo baz
Unknown command 'foo=bar' - try 'help'

---

 common/cli_hush.c    | 2 +-
 test/cmd/test_echo.c | 2 ++
 2 files changed, 3 insertions(+), 1 deletion(-)
common/cli_hush.c +1 -1
index b7f0f0ff41..1b9bef64b6 100644
@@ -1672,7 +1672,7 @@ static int run_pipe_real(struct pipe *pi)
 			return -1;
 		}
 		/* Process the command */
-		return cmd_process(flag, child->argc, child->argv,
+		return cmd_process(flag, child->argc - i, child->argv + i,
 				   &flag_repeat, NULL);
 #endif
 	}
test/cmd/test_echo.c +0 -0
index 4183cf75bb..13e1fb7c82 100644

test/cmd/test_echo.c static struct test_data echo_data[] = { · line 40

Quoted 8 lines
test/cmd/test_echo.c +2 -0
@@ -33,6 +33,8 @@ static struct test_data echo_data[] = {
 	 */
 	{"setenv jQx X; echo \"a)\" ${jQx} 'b)' '${jQx}' c) ${jQx}; setenv jQx",
 	 "a) X b) ${jQx} c) X"},
+	/* Test shell variable assignments without substitutions */
+	{"foo=bar echo baz", "baz"},
 	/* Test handling of shell variables. */
 	{"setenv jQx; for jQx in 1 2 3; do echo -n \"${jQx}, \"; done; echo;",
 	 "1, 2, 3, "},

OK, I'm really confused now. With and without this patch, in sandbox I see:
=> foo=bar echo $foo
bar
=> echo $foo
bar

Which doesn't match dash, busybox-sh nor bash 4.4.20, all of which do something like:

$ foo=bar echo $foo
$ echo $foo

$

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20210301/a57b77f6/attachment.sig>
#12 lore raw

PATCH 1/1 hush: Fix assignments being misinterpreted as commands

In reply to Sean Anderson
Quoted 8 lines · Sean Anderson

If there were no variable substitutions in a command, then initial assignments would be misinterpreted as commands, instead of being skipped over. This is demonstrated by the following example:

=> foo=bar echo baz
Unknown command 'foo=bar' - try 'help'

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20210413/7c3f2795/attachment.sig>