bug-bash
[Top][All Lists]
Advanced

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

Re: "printf %d ''" should diagnose the empty string


From: Chet Ramey
Subject: Re: "printf %d ''" should diagnose the empty string
Date: Wed, 20 Nov 2024 11:31:20 -0500
User-agent: Mozilla Thunderbird

On 11/20/24 10:31 AM, Martin D Kealey wrote:


On Wed, 20 Nov 2024, 07:36 Paul Eggert, <eggert@cs.ucla.edu <mailto:eggert@cs.ucla.edu>> wrote:

    However, I don't see why Bash should differ from POSIX behavior. Bash
    already diagnoses "printf %d x" and "printf %d ' '"; why should it be
    silent about "printf %d ''"?


 From a semantic perspective, because "" doesn't contain any non-digits.

POSIX says this doesn't matter, it's whether or not the argument can be
`completely converted':

"If an argument operand cannot be completely converted into an internal
value appropriate to the corresponding conversion specification, a
diagnostic message shall be written to standard error and the utility shall
not exit with a zero exit status..."

The question is what `completely converted' means here.

As for the argument itself, it has to be "an unsuffixed C integer constant
as as described by the ISO C standard", which requires at least one digit
(or letter for hex constants).


From an implementation perspective, the standard check for a valid number is to call strtol() (or similar) and then see if there are any non-digits remaining. It takes an extra check to see if it stopped without processing any digits.

If you want to use strtol() as a reference, POSIX says that it doesn't
perform any conversion if the string is null:

"If the subject sequence is empty or does not have the expected form, no
conversion is performed; the value of nptr shall be stored in the object
pointed to by endptr, provided that endptr is not a null pointer."

So that can reasonably mean "not completely converted."

I'm in favour of treating the empty token (string) as being a valid way to write zero, in the same way that 00 and 0000000 are also valid ways to write zero. That makes it more consistent in ways that are useful.

I don't think being able to write "$var" without checking whether or not
var is unset or null is compelling enough.

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]