Re: [BUG] one-shot variables aren't propagated past functions

2020-04-30 Thread Michael Greenberg
On 2020-04-30 06:00:01, Denton Liu  wrote:

> Hi all,
>
> I believe that I've encountered a bug in dash 0.5.10.2. With the
> following input
>
> f () {
> echo $var
> }
>
> var=test f
> echo $var
>
> I would expect the output to be
>
> test
> test
>
> but the output is actually
>
> test
> 
>
> It seems like this violates the POSIX spec.

Junio's reading of the spec isn't quite right. From Section 2.9.1
"Simple Commands":

  If the command name is a function that is not a standard utility
  implemented as a function, variable assignments shall affect the
  current execution environment during the execution of the function. It
  is unspecified:

  - Whether or not the variable assignments persist after the completion
of the function

  - Whether or not the variables gain the export attribute during the
execution of the function

  - Whether or not export attributes gained as a result of the variable
assignments persist after the completion of the function (if
variable assignments persist after the completion of the function)

I've seen only two ways that shells treat simple commands of the form
`VAR=VAL FUNC ARGS ...`. Shells with support for the (unspecified)
`local` command tend to have the behavior dash does, where the
assignment of `VAR` is local in scope. Other shells treat such
assignments globally.

You can find a summary of this issue in my recent paper from POPL 2020,
"Executable formal semantics for the POSIX shell", Section
8.3. (https://mgree.github.io/papers/popl2020_smoosh.pdf)

Cheers,
Michael


Quoting Junio C Hamano[1]:
>> Johannes Sixt  writes:
>>
>> > Tarmigan Casebolt schrieb:
>> >> REQUEST_METHOD="GET" some_shell_function
>> >
>> >> I can't tell from my reading of the POSIX spec whether my usage was
>> >> wrong or if dash is wrong,
>> >
>> > According to POSIX, variables set as shown above for shell functions are
>> > not exported and retain their value after the function returns.
>>
>> I actually looked for this yesterday, but didn't find a relevant
>> definition.  But "2.9.5 Function Definition Command" [*1*] seems to
>> address the issue: "When a function is executed, it shall have the
>> syntax-error and variable-assignment properties described for special
>> built-in utilities...".
>>
>> And "2.14 Special Built-in Utilities" section [*2*] says "2. Variable
>> assignments specified with special built-in utilities remain in effect
>> after the built-in completes...".  Taking both together, it seems that
>> the assignment should be in effect after the function returns.
>>
> [...]
>>
>> [References]
>>
>> *1* 
>> https://nam01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.opengroup.org%2Fonlinepubs%2F9699919799%2Futilities%2FV3_chap02.html%23tag_18_09_05data=02%7C01%7CMichael.Greenberg%40pomona.edu%7Cb8c8aa7c728f4110ab8208d7eced43f4%7C817f590439044ee8b3a5a65d4746ff70%7C0%7C0%7C637238376114210837sdata=ZDuZDf%2B2fbEbehYK9DpFmHHhtFwEjUh51pQwdexANdk%3Dreserved=0
>> *2* 
>> https://nam01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.opengroup.org%2Fonlinepubs%2F9699919799%2Futilities%2FV3_chap02.html%23tag_18_14data=02%7C01%7CMichael.Greenberg%40pomona.edu%7Cb8c8aa7c728f4110ab8208d7eced43f4%7C817f590439044ee8b3a5a65d4746ff70%7C0%7C0%7C637238376114210837sdata=2NGIBN2kWLb%2FwZRy5lh021vUd%2FVLXEh2IzNq0lpv0R4%3Dreserved=0
>
> It also seems like on bash 5.0.16, running with --posix produces the
> expected output, although without --posix it produces the blank line.
>
> Originally discussed here[2].




> Thanks,
>
> Denton
>
> [1]: 
> https://nam01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flore.kernel.org%2Fgit%2F7vljfzz0yd.fsf%40alter.siamese.dyndns.org%2Fdata=02%7C01%7CMichael.Greenberg%40pomona.edu%7Cb8c8aa7c728f4110ab8208d7eced43f4%7C817f590439044ee8b3a5a65d4746ff70%7C0%7C0%7C637238376114210837sdata=bQcTgwa%2Bb%2FobsCT5L2laRqP%2BmhJQNJgCHllsVHNcwhw%3Dreserved=0
> [2]: 
> https://nam01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flore.kernel.org%2Fgit%2F20200430092220.GA25331%40generichostname%2Fdata=02%7C01%7CMichael.Greenberg%40pomona.edu%7Cb8c8aa7c728f4110ab8208d7eced43f4%7C817f590439044ee8b3a5a65d4746ff70%7C0%7C0%7C637238376114220834sdata=cEpyKOzR6UB%2Bvdr%2FOK7zsex1aRj7Fqyng5ZpH9ZDvSc%3Dreserved=0
> 
>
> [EXTERNAL EMAIL] Exercise caution before clicking on links or opening 
> attachments.


[BUG] one-shot variables aren't propagated past functions

2020-04-30 Thread Denton Liu
Hi all,

I believe that I've encountered a bug in dash 0.5.10.2. With the
following input

f () {
echo $var
}

var=test f
echo $var

I would expect the output to be

test
test

but the output is actually

test


It seems like this violates the POSIX spec. Quoting Junio C Hamano[1]:
> Johannes Sixt  writes:
> 
> > Tarmigan Casebolt schrieb:
> >> REQUEST_METHOD="GET" some_shell_function
> >
> >> I can't tell from my reading of the POSIX spec whether my usage was
> >> wrong or if dash is wrong,
> >
> > According to POSIX, variables set as shown above for shell functions are
> > not exported and retain their value after the function returns.
> 
> I actually looked for this yesterday, but didn't find a relevant
> definition.  But "2.9.5 Function Definition Command" [*1*] seems to
> address the issue: "When a function is executed, it shall have the
> syntax-error and variable-assignment properties described for special
> built-in utilities...".
> 
> And "2.14 Special Built-in Utilities" section [*2*] says "2. Variable
> assignments specified with special built-in utilities remain in effect
> after the built-in completes...".  Taking both together, it seems that
> the assignment should be in effect after the function returns.
> 
[...]
> 
> [References]
> 
> *1* 
> http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_05
> *2* 
> http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_14

It also seems like on bash 5.0.16, running with --posix produces the
expected output, although without --posix it produces the blank line.

Originally discussed here[2].

Thanks,

Denton

[1]: https://lore.kernel.org/git/7vljfzz0yd@alter.siamese.dyndns.org/
[2]: https://lore.kernel.org/git/20200430092220.GA25331@generichostname/