defensive-coding-guide/modules/ROOT/examples/C-Arithmetic-add.adoc
2022-01-13 20:42:40 +01: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;
}