[Top][All Lists]

[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
CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='i586' -DCONF_OSTYPE='linux-gnu' -
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
Machine Type: i586-pc-linux-gnu

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

        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?

        echo $((2147483648/1024))

        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.



reply via email to

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