defensive-coding-guide/modules/ROOT/pages/_partials/snippets/C-Arithmetic-add.adoc
2018-09-20 11:51:33 +02:00

17 lines
250 B
Text

void report_overflow(void);
int
add(int a, int b)
{
int result = a + b;
if (a < 0 || b < 0) {
return -1;
}
// The compiler can optimize away the following if statement.
if (result < 0) {
report_overflow();
}
return result;
}