[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
bug#8545: issues with recent doprnt-related changes
From: |
Paul Eggert |
Subject: |
bug#8545: issues with recent doprnt-related changes |
Date: |
Tue, 03 May 2011 13:24:16 -0700 |
User-agent: |
Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.15) Gecko/20110421 Fedora/3.1.9-2.fc14 Thunderbird/3.1.9 |
>> There are similar reliable tests for the other arithmetic operations.
>
> Is this documented somewhere? Is there a list of the standard ways?
CERT has something, here:
https://www.securecoding.cert.org/confluence/display/seccode/INT32-C.+Ensure+that+operations+on+signed+integers+do+not+result+in+overflow
Although the principles in that memo are OK, the actual code is
hard to read and its multiplication overflow checking is buggy.
Here's something better, which I just now wrote. Also, please see
Emacs Bug#8611 <http://debbugs.gnu.org/cgi/bugreport.cgi?bug=8611>;
its patch uses code like the following.
#include <limits.h>
int
add_overflow (int a, int b)
{
return (b < 0
? a < INT_MIN - b
: INT_MAX - b < a);
}
int
subtract_overflow (int a, int b)
{
return (b < 0
? INT_MAX + b < a
: a < INT_MIN + b);
}
int
unary_minus_overflow (int a)
{
return a < -INT_MAX;
}
int
multiply_overflow (int a, int b)
{
return (b < 0
? (a < 0
? a < INT_MAX / b
: b != -1 && INT_MIN / b < a)
: (b != 0
&& (a < 0
? a < INT_MIN / b
: INT_MAX / b < a)));
}
int
quotient_overflow (int a, int b)
{
/* This does not check for division by zero. Add that if you like. */
return a < -INT_MAX && b == -1;
}
int
remainder_overflow (int a, int b)
{
/* Mathematically the remainder should never overflow, but on x86-like
hosts INT_MIN % -1 traps, and the C standard permits this. */
return quotient_overflow (a, b);
}
bug#8545: issues with recent doprnt-related changes, Paul Eggert, 2011/05/04
- bug#8545: issues with recent doprnt-related changes, Eli Zaretskii, 2011/05/04
- bug#8545: issues with recent doprnt-related changes, Paul Eggert, 2011/05/04
- bug#8545: issues with recent doprnt-related changes, Eli Zaretskii, 2011/05/05
- bug#8545: issues with recent doprnt-related changes, Stefan Monnier, 2011/05/06
- bug#8545: issues with recent doprnt-related changes, Paul Eggert, 2011/05/06
- bug#8545: issues with recent doprnt-related changes, Eli Zaretskii, 2011/05/06
- bug#8545: issues with recent doprnt-related changes, Stefan Monnier, 2011/05/06
- bug#8545: issues with recent doprnt-related changes, Eli Zaretskii, 2011/05/06
- bug#8545: issues with recent doprnt-related changes, Stefan Monnier, 2011/05/06