News for hackers. Stuff that matters.
Re:C and C++ Prioritize Performance over Correctness by coliveira on Friday August 18, 2023 @01:47PM (#37179422)
You cannot check for overflow like this, because you're causing it. There are other ways to do this without adding first:

  #include <limits.h>

  int safe_add(int a, int b) {
    if (a > 0 && b > INT_MAX - a) {
        /* deal with overflow... */
    } else if (a < 0 && b < INT_MIN - a) {
        /* deal underflow.. */
    }
    return a + b;
  }

There are no comments on this comment yet.