bug-bash
[Top][All Lists]
Advanced

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

arithmetic evaluation integer overflow


From: Joeri Capens
Subject: arithmetic evaluation integer overflow
Date: Fri, 27 Sep 2002 17:02:16 +0200

Configuration Information [Automatically generated, do not change]:
Machine: i586
OS: linux-gnu
Compiler: gcc
Compilation
CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='i586' -DCONF_OSTYPE='linux-gnu' -
DCONF_MACHTYPE='i586-pc-linux-gnu' -DCONF_VENDOR='pc' -DSHELL -DHAVE_CONFIG_
H  -I.  -I. -I./include -I./lib  -g -O2
uname output: Linux server 2.4.18 #4 Fri May 31 01:25:31 PDT 2002 i586
unknown
Machine Type: i586-pc-linux-gnu

Bash Version: 2.05b
Patch Level: 0
Release Status: release

Description:
        Although the manual states that there is no overflow check on
arithmetic expressions, I think there should be. I've written a shell script
that reads the download value from /proc/net/dev, and once this value (in
bytes) was bigger than about 2 gigabyte (2^31 bytes), the value returned by
a simple arithmetic expression turned out to be neagative! It took me a
while to realize this is because signed integers can turn negative at
overflow. 2^31 looks like a large number, but it doesn't when you write it
in gigabytes. I know there are arithmetic programs like bc that return the
correct value, but this doesn't mean bash shouldn't, right?

Repeat-By:
        echo $((2147483648/1024))

Fix:
        You could trap an overflow just like trapping a division by zero, at
least this won't give people a terribly wrong answer to, for example, a
simple division. Also, it may not be a bad idea to use long integers
instead of integers, so people can evaluate very large numbers correctly
without having to use another program like bc.

regards,

Joeri





reply via email to

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