https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91555
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |RESOLVED
Resolution|--- |INVALID
--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
static int
increment_overflow(int *number, int delta)
{
int number0;
number0 = *number;
*number += delta;
return (*number < number0) != (delta < 0);
}
static int
long_increment_overflow(long *number, int delta)
{
long number0;
number0 = *number;
*number += delta;
return (*number < number0) != (delta < 0);
}
These all have undefined behavior with respect to signed overflow.
You either need to use unsigned types to do the addition and then see if there
was an overflow or you need to check for the overflow before it happens.
The latest code (from
https://github.com/epam/libdt/blob/master/src/unix/libtz/libtz.c at least) has:
static int
long_increment_overflow(lp, m)
long *const lp;
int const m;
{
register long const l = *lp;
if ((l >= 0) ? (m > LONG_MAX - l) : (m < LONG_MIN - l)) {
return TRUE;
}
*lp += m;
return FALSE;
}