I'm trying to check if signed addition would overflow. Generally, to check if
int a + int b
would overflow (a and b both positive), I check if
if (a > INT_MAX - b)
But now I want to check if
int a + int b - int c
would overflow. I know that a, b, and c are positive and that b >= c, so I do the following check:
if (a > INT_MAX - b + c)
Now my question is, can compiler rewrite
INT_MAX - b + c to INT_MAX + c - b ?
My concern is, then it will first do INT_MAX + c, which may overflow and can result into undefined behavior.