[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: built-in printf returns success when integer is out of range
From: |
thomas |
Subject: |
Re: built-in printf returns success when integer is out of range |
Date: |
Wed, 26 Jul 2023 14:14:32 -0700 |
On Wed, 26 Jul 2023 21:52:32 +0100, Dennis Williamson
<dennistwilliamson@gmail.com> said:
>> Configuration Information [Automatically generated, do not change]:
>> Machine: x86_64
>> OS: linux-gnu
>> Compiler: gcc
>> Compilation CFLAGS: -g -O2 -fstack-protector-strong -Wformat
>> -Werror=format-security -Wall
>> uname output: Linux fnord42 6.1.25-1rodete1-amd64 #1 SMP
>> PREEMPT_DYNAMIC Debian 6.1.25-1rodete1 (2023-05-11) x86_64 GNU/Linux
>> Machine Type: x86_64-pc-linux-gnu
>>
>> Bash Version: 5.2
>> Patch Level: 15
>> Release Status: release
>>
>> Description:
>> printf '%d\n' 111111111111111111111111111 && echo success
>> prints "success"
>> /usr/bin/printf does not, but instead returns EXIT_FAILURE (1).
>>
>> Repeat-By:
>> Run:
>> printf '%d\n' 111111111111111111111111111 && echo success
>>
>>
> You didn't include all the output. It's treated as a warning rather than an
> error. That's why an error status isn't set.
Ah indeed, the stderr message with bash builtin has the word
"warning", whereas the Linux coreutils one does not[1].
Regardless, I do not believe this should be a warning, and instead
that coreutils is doing the right thing, and bash is not.
The builtin was given a request to print an integer. It failed to do
that, because of invalid input. It's wrong to silently do the wrong
thing. I say silently, but what I mean is without a way to handle
the error, not that stderr didn't get any warning.
Scripts can be unattended, and they should fail if they fail, not
continue with best effort.
Not that 2^63-1 is any kind of best effort.
Bash built-in printf should fail (EXIT_FAILURE) on the integer out of
range just like it does fail given something that's not a number at
all.
[1]
$ printf '%d\n' 11111111111111111111111 && echo yes || echo no
bash: printf: warning: 11111111111111111111111: Numerical result out of range
9223372036854775807
yes
$ /usr/bin/printf '%d\n' 11111111111111111111111 && echo yes || echo no
/usr/bin/printf: ‘11111111111111111111111’: Numerical result out of range
9223372036854775807
no
--
typedef struct me_s {
char name[] = { "Thomas Habets" };
char email[] = { "thomas@habets.se" };
char kernel[] = { "Linux" };
char *pgpKey[] = { "http://www.habets.pp.se/pubkey.txt" };
char pgp[] = { "9907 8698 8A24 F52F 1C2E 87F6 39A4 9EEA 460A 0169" };
char coolcmd[] = { "echo '. ./_&. ./_'>_;. ./_" };
} me_t;