[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Bug in shell: buffer overflow.
From: |
Bob Proulx |
Subject: |
Re: Bug in shell: buffer overflow. |
Date: |
Fri, 31 Dec 2010 11:33:11 -0700 |
User-agent: |
Mutt/1.5.20 (2009-06-14) |
nz@lavabit.com wrote:
> echo $((256**8))
> echo $((72057594037927936*128))
> echo $((1000000000000000000000000))
> etc.
Unless great effort is made to perform math in arbitrary precision
http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic
all computer arithmetic is done using native machine integers. As
documented in the bash manual in the ARITHMETIC EVALUATION section:
ARITHMETIC EVALUATION
Evaluation is done in fixed-width integers with no check for
overflow, though division by 0 is trapped and flagged as an
error.
Your expressions above are overflowing the value of your system's
maximum integer size. You can read the system's maximum integer size
using getconf.
$ getconf INT_MAX
2147483647
Fixed integer calculations are native to the system hardware and very
fast and most of the time sufficient for most tasks.
If you need larger numbers then you would need to use a tool that
supports arbitrary precision arithmetic such as 'bc'. The bc tool may
be called from the shell to perform your calculations. It is a
powerful calculator.
$ man bc
$ echo $(echo "72057594037927936*128" | bc -l)
9223372036854775808
Bob