#973620 bash: overflow on integer variables greater than 9223372036854775807

Package:
bash
Source:
bash
Description:
GNU Bourne Again SHell
Submitter:
Antonio
Date:
2024-04-15 13:21:02 UTC
Severity:
normal
Tags:
#973620#5
Date:
2020-11-02 15:46:14 UTC
From:
To:
Dear Maintainer,
recently while I was running some tests, I ran into this strange overflow:

$ declare -i n=9223372036854775800; for((i=0; i<15; ++i)); do echo "$i -> $n";
n+=1; done

0 -> 9223372036854775800
1 -> 9223372036854775801
2 -> 9223372036854775802
3 -> 9223372036854775803
4 -> 9223372036854775804
5 -> 9223372036854775805
6 -> 9223372036854775806
7 -> 9223372036854775807
8 -> -9223372036854775808
9 -> -9223372036854775807
10 -> -9223372036854775806
11 -> -9223372036854775805
12 -> -9223372036854775804
13 -> -9223372036854775803
14 -> -9223372036854775802

The integer handled by bash is obviously very large, but I believe
that in the event of an overflow it would be better to reset the
variable and issue an error flow warning, rather than remain silent.

Thanks,
Antonio

#973620#10
Date:
2024-04-15 11:59:10 UTC
From:
To:
Control: found -1 5.2.21-2
Control: tags -1 upstream
X-Debbugs-CC: bug-bash@gnu.org

Bash 5.2.21 is affected by this issue:

     $ declare -i n=$((2**63 - 2))
     $ for i in {1..4}; do echo "$i -> $n"; n+=1; done
     1 -> 9223372036854775806
     2 -> 9223372036854775807
     3 -> -9223372036854775808
     4 -> -9223372036854775807

     $ declare -i n=36893488147419103234; echo $?
     0
     $ echo $n
     2

Would it be possible to detect this overflow (or non-representable
numbers, like in the second case) and warn about it?

Regards,

#973620#23
Date:
2024-04-15 13:12:19 UTC
From:
To:
Hi. Thanks for the report. As documented, bash integer arithmetic doesn't
check for overflow (it never has). Adding such checks is a low-priority
item.

Chet