Illegal function names are accepted after being used as aliases
Function definitions that use a bad function name (such as "-" and "=") are accepted if the function name already exists as an alias. For example:
$ -
dash: 1: -: not found
$ - () { echo hello; }
dash: 2: Syntax error: Bad function name
$ -
dash: 2: -: not found
$ alias -=true
$ -
$ - () { echo hello; }
$ -
hello
$Quoted 2 lines · Jan Verbeek
Function definitions that use a bad function name (such as "-" and "=") are accepted if the function name already exists as an alias. For example:
Not necessarily a bug.
Quoted 9 lines
$ -
dash: 1: -: not found
$ - () { echo hello; }
dash: 2: Syntax error: Bad function name
$ -
dash: 2: -: not found
$ alias -=true
$ -This is equivalent to running 'true'.
Quoted 1 line
$ - () { echo hello; }This is equivalent to running 'true () { echo hello; }' - the alias expansion happens BEFORE the function definition is even parsed. You are NOT defining a function named '-', but one named 'true'.
Quoted 1 line
$ -
This is again equivalent to running 'true' - except that now the function name 'true' exists and bypasses the shell builtin.
Quoted 2 lines
hello
$
So the only thing remaining is to determine if it is legal to have a function override the name of a regular shell builtin. But
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_01
under "Command Search and Execution" states that function names have priority over regular built-ins (so yes, creating a function named 'true' is doable, although stupid).
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Quoted 15 lines · Jan Verbeek
Function definitions that use a bad function name (such as "-" and "=") are accepted if the function name already exists as an alias. For example:
$ - dash: 1: -: not found $ - () { echo hello; } dash: 2: Syntax error: Bad function name $ - dash: 2: -: not found $ alias -=true $ - $ - () { echo hello; } $ - hello $
After alias -=true, - () { echo hello; } is treated as a use of that alias. It doesn't define a function with a name of -, it defines a function with a name of true, which consists only of valid characters.
$ alias -=true
$ -() { echo hello; }
$ type -
- is an alias for true
$ type true
true is a shell function
$ true
helloThis matches bash's behaviour, aside from bash requiring -- to prevent detection of invalid flags to the alias command:
bash-4.3$ alias -- -=true
bash-4.3$ -() { echo hello; }
bash-4.3$ type -
- is aliased to `true'
bash-4.3$ type true
true is a function
true ()
{
echo hello
}
bash-4.3$ true
hello
Cheers,
Harald van Dijk
Quoted 4 lines · Harald van Dijk
This matches bash's behaviour, aside from bash requiring -- to prevent detection of invalid flags to the alias command:
bash-4.3$ alias -- -=true
Then dash DOES have a bug:
# dash $ alias -- -='echo hi' alias: -- not found $ echo $? 1 $ - hi $
POSIX XCU 1.4 is clear:
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap01.html
"Default Behavior: When this section is listed as "None.", it means that the implementation need not support any options. Standard utilities that do not accept options, but that do accept operands, shall recognize "--" as a first argument to be discarded."
and alias takes operands, stating "OPTIONS: None.", which means POSIX _requires_ 'alias -- -=name' to (attempt to) define only the single alias '-', and NOT to also attempt to define '--' as an alias.
It's okay if dash allows 'alias -=blah' to define '-' as an alias as an extension, but it MUST ignore '--' the way bash does.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Quoted 8 lines · Eric Blake
Quoted 4 lines · Harald van Dijk
This matches bash's behaviour, aside from bash requiring -- to prevent detection of invalid flags to the alias command:
bash-4.3$ alias -- -=true
Then dash DOES have a bug:
Indeed, I wasn't trying to suggest otherwise, my apologies if it came across that way. It's not limited to the alias command though, I spotted at least the exit and getopts commands having the same problem, and it should probably be fixed for all of them at once.
Cheers,
Harald van Dijk
Quoted 6 lines · Harald van Dijk
Quoted 5 lines · Eric Blake
Quoted 3 lines · Harald van Dijk
This matches bash's behaviour, aside from bash requiring -- to prevent detection of invalid flags to the alias command:
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
bash-4.3$ alias -- -=true
Then dash DOES have a bug:
Indeed, I wasn't trying to suggest otherwise, my apologies if it came across that way. It's not limited to the alias command though, I spotted at least the exit and getopts commands having the same problem, and it should probably be fixed for all of them at once.
getopts - definitely needs a fix
exit - fuzzy. exit is a special built-in (unlike getopts); and XCU 2.14 states:
"Some of the special built-ins are described as conforming to XBD
Utility Syntax Guidelines. For those that are not, the requirement in Utility Description Defaults that "--" be recognized as a first argument to be discarded does not apply and a conforming application shall not use that argument. "
Conforming apps cannot expect 'exit -1' to work, and therefore, cannot also expect 'exit -- -1' to work, since the only standards-defined values for an argument to exit is a non-negative decimal integer less than 256. Of course, if you want to fix it along with all the others, that's fine; I'm just pointing out that 'exit' isn't broken as-is.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Quoted 9 lines · Eric Blake
exit - fuzzy. exit is a special built-in (unlike getopts); and XCU 2.14 states:
"Some of the special built-ins are described as conforming to XBD
Utility Syntax Guidelines. For those that are not, the requirement in Utility Description Defaults that "--" be recognized as a first argument to be discarded does not apply and a conforming application shall not use that argument. "
Quoted 5 lines
Conforming apps cannot expect 'exit -1' to work, and therefore, cannot also expect 'exit -- -1' to work, since the only standards-defined values for an argument to exit is a non-negative decimal integer less than 256. Of course, if you want to fix it along with all the others, that's fine; I'm just pointing out that 'exit' isn't broken as-is.
I was under the impression that the intent from the dash side was to handle all commands the same, and that impression was based on the fact that the . command has received additional code to handle -- even though there's no requirement for that. However, looking into the original bug report that prompted that change in more detail I see that the standard will very likely require support for -- in the . command in the future, so that doesn't hold up.
If that intent isn't there (I'm not saying it's not; I'm unsure now), the list of utilities that should be extended is far smaller, if I'm not overlooking anything:
- alias
- getopts
- type
- exec?
- local?
exec is like .: there's currently no requirement to support --, but that requirement is likely to come in the future.
local is currently non-standard and it's hard to guess whether it will require support for -- if standardised.
Cheers,
Harald van Dijk
Quoted 8 lines · Harald van Dijk
I was under the impression that the intent from the dash side was to handle all commands the same, and that impression was based on the fact that the . command has received additional code to handle -- even though there's no requirement for that. However, looking into the original bug report that prompted that change in more detail I see that the standard will very likely require support for -- in the . command in the future, so that doesn't hold up.
Here's the link for dot and exec supporting --:
http://austingroupbugs.net/view.php?id=252
Quoted 9 lines
If that intent isn't there (I'm not saying it's not; I'm unsure now), the list of utilities that should be extended is far smaller, if I'm not overlooking anything:
- alias
- getopts
- type
- exec?
- local?
Weird that unalias already works. Oh, because of 'unalias -a'. I didn't spot any others that you missed (doesn't mean there aren't any, just that I didn't spot them).
Quoted 3 lines
exec is like .: there's currently no requirement to support --, but that requirement is likely to come in the future.
See the above link; exec must support -- if '.' does. I also found
http://austingroupbugs.net/view.php?id=163 which confirms that 'eval' is not required (nor it is prevented) from recognizing --. There's also
http://austingroupbugs.net/view.php?id=960 which mentioned the exit status of export and several other special builtins, but added no requirements related to --.
Quoted 3 lines
local is currently non-standard and it's hard to guess whether it will require support for -- if standardised.
If standardized, I expect it to require support for --, on the grounds that 'local -r' already has meaning in bash, so local is definitely a candidate for taking options.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org