Check for overflow on addition, subtraction, multiplication of integers

This commit is contained in:
Dianne Skoll
2021-01-30 12:56:37 -05:00
parent 9c287e3fd7
commit 0a9eb07f6f
3 changed files with 32 additions and 0 deletions

View File

@@ -123,3 +123,20 @@ int DateOK(int y, int m, int d)
d > DaysInMonth(m, y) ) return 0;
else return 1;
}
/* Functions designed to defeat gcc optimizer */
int _private_div(int a, int b) { return a/b; }
int _private_add_overflow(int result, int b, int old)
{
if (b > 0 && result < old) return 1;
if (b < 0 && result > old) return 1;
return 0;
}
int _private_sub_overflow(int result, int b, int old)
{
if (b < 0 && result < old) return 1;
if (b > 0 && result > old) return 1;
return 0;
}