Bug 251770 - /bin/sh: false expansion
Summary: /bin/sh: false expansion
Status: New
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: 12.1-STABLE
Hardware: Any Any
: --- Affects Only Me
Assignee: freebsd-bugs (Nobody)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-12-12 00:28 UTC by Steffen (Daode) Nurpmeso
Modified: 2021-01-05 19:10 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Steffen (Daode) Nurpmeso 2020-12-12 00:28:54 UTC
Hi.

On 11.2 and 12.1 i get false expansion for

  /bin/sh -c 'du=ich wir='"'"'hey '"'"'$du; echo $wir'

It echos hey but should echo "hey ich".
Comment 1 Jilles Tjoelker freebsd_committer 2021-01-02 17:27:43 UTC
Given the standard as modified by https://www.austingroupbugs.net/view.php?id=1123 I see insufficient reason to change this, also to avoid POLA issues with people trying to swap via a=$b b=$a.
Comment 2 Steffen (Daode) Nurpmeso 2021-01-02 21:07:53 UTC
Sorry for being too terse.
It was just a simplemost reproducer without any further comments.
Sorry.

#?2|kent$ for s in dash bash mksh bosh; do $s -c 'du=ich wir='"'"'hey '"'"'$du; echo $wir'; done
hey ich
hey ich
hey ich
hey ich

Hmmmm.  Since issue 1123 is possibly not, you surely mean instead

75501          Variable assignments shall be performed as follows:
75502             •   If no command name results, variable assignments shall affect the current execution
75503                 environment.
75504             •   If the command name is not a special built-in utility or function, the variable assignments
75505                 shall be exported for the execution environment of the command and shall not affect the
75506                 current execution environment except as a side-effect of the expansions performed in step
75507                 4. In this case it is unspecified:
75508                   — Whether or not the assignments are visible for subsequent expansions in step 4
75509                   — Whether variable assignments made as side-effects of these expansions are visible for
75510                     subsequent expansions in step 4, or in the current shell execution environment, or
75511                     both

where 4 is

5495                    4.    Each variable assignment shall be expanded for tilde expansion, parameter expansion,
75496                          command substitution, arithmetic expansion, and quote removal prior to assigning the
75497                          value.

So .. this very exact behaviour is unspecified?  If that is really true (i personally have hard times with standard texts), this issue can very well be closed.
(FreeBSD sh is alone with this exact behaviour as far as i know, still..)
Comment 3 Jilles Tjoelker freebsd_committer 2021-01-03 13:52:25 UTC
I think you are right; the formulation "Each variable assignment shall be expanded prior to assigning the value" is not compatible with our behaviour to expand everything before assigning anything.

On the other hand, a=$b b=$a U where U is not empty and neither a special builtin nor a function can still place swapped values in the utility environment (although it is not required to).
Comment 4 Steffen (Daode) Nurpmeso 2021-01-05 19:10:48 UTC
Sorry for being so late, i wanted to reread the standard text, maybe to open an issue.  But i think your last statement pretty much describes the situation.  (Whereas my initial report was only "from gut".)

3385   4.23        Variable Assignment
3386               In the shell command language, a word consisting of the following parts:
3387               varname=value
3388               When used in a context where assignment is defined to occur and at no other time, the value
3389               (representing a word or field) shall be assigned as the value of the variable denoted by varname.
3390               Note:          For further information, see XCU Section 2.9.1 (on page 2365).

---

75482   2.9.1        Simple Command

75495                    4.    Each variable assignment shall be expanded for tilde expansion, parameter expansion,
75496                          command substitution, arithmetic expansion, and quote removal prior to assigning the
75497                          value.

---

75501          Variable assignments shall be performed as follows:
75502             •   If no command name results, variable assignments shall affect the current execution
75503                 environment.

---

So everything should be handled sequentially, making it a bug.

---

75504             •   If the command name is not a special built-in utility or function, the variable assignments
[.]
75507                 4. In this case it is unspecified:
75508                   — Whether or not the assignments are visible for subsequent expansions in step 4
75509                   — Whether variable assignments made as side-effects of these expansions are visible for
75510                     subsequent expansions in step 4, or in the current shell execution environment, or
75511                     both

---

So it allows to setup the "execution environment of the command" entirely from the current environment, which is effectively read-only.  As you say.