Re: [PATCH] fix "Illegal number" on FreeBSD & macOS for x=; echo $((x))

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Op 06-03-18 om 00:23 schreef Harald van Dijk:
> On 3/6/18 12:23 AM, Martijn Dekker wrote:
>> "All variables shall be initialized to zero if they are not otherwise
>> assigned by the input to the application."
> 
> In your example, x has been assigned. Its value is empty, but empty and
> unset are two different things.
> 
> But I see now that dash gives the same error when x is unset. That part
> is definitely wrong for exactly the reason you point out.

By default, all unset variables are considered empty, so they should act
the same.

That brings me to another possible issue, though:

$ dash -uc 'unset x; echo $((x))'
0

Shouldn't that give an "parameter not set" error under set -u?
Interestingly, only bash and AT&T ksh93 currently do that.


>> Also, consensus among existing shells appears to be universal.
> 
> Mostly, but not fully. yash is the exception:
> 
>   $ yash -c 'unset x; echo $((x))'
>   0
>   $ yash -c 'x=; echo $((x))'
> 


But in POSIX mode it acts like other shells:

$ yash -o posix -c 'x=; echo $((x))'
0


> yash is pretty special when it comes to shell arithmetic:
> 
>   $ yash -c 'x=abc; echo $((x))'
>   abc
>   $ yash -c 'x=abc; echo $((x+0))'
>   yash: arithmetic: `abc' is not a valid number


$ yash -o posix -c 'x=abc; echo $((x))'
yash: arithmetic: `abc' is not a valid number


>>> But perhaps
>>>
>>>    if (errno == ERANGE)
>>>
>>> is all that's needed here.
>>
>> Explain?
> 
> Since base is always a constant 0 or a constant 10, never a
> user-provided value, the only error that strtoimax will ever report on
> glibc systems is ERANGE. Checking only ERANGE therefore preserves the
> glibc behaviour, and allows the exact same set of errors to be detected
> on non-glibc systems.


That makes sense, thanks.

- M.
--
To unsubscribe from this list: send the line "unsubscribe dash" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [LARTC]     [Bugtraq]     [Yosemite Forum]     [Photo]

  Powered by Linux