Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: formatting, removed CHAR_BIT

...

Code Block
bgColor#ccccff
langc
#include <limits.h>
 
signed int si_a;
signed int si_b;
signed int sum;

void (func) {
  /* Initialize si_a, si_b and sum */

  if (((si_b > 0) && (si_a > (INT_MAX - si_b))) ||
     || ((si_b < 0) && (si_a < (INT_MIN - si_b)))) {
    /* Handle error condition */
  } else {
    sum = si_a + si_b;
  }
  /* ... */
}

...

Code Block
bgColor#ccccff
langc
#include <limits.h>
 
signed int si_a;
signed int si_b;
signed int result;

void func(void) {
  /* Initialize si_a, si_b and result */

  if (((si_a^si_b) &
    &   (((si_a ^ ((si_a^si_b)
 & INT_MIN)) -
 & (1 << (sizeof(int)  * CHAR_BIT -1)))) - si_b)^si_b)) < 0) {
     /* Handle error condition */
  } else {
    result = si_a - si_b;
  }

  /* ... */
}

...

Code Block
bgColor#ccccff
langc
#include <limits.h>
 
signed int si_a;
signed int si_b;
singed int result;

void func(void) {
  /* Initialize si_a, si_b and result */

  if ((si_b > 0 && si_a < INT_MIN + si_b) ||
      (si_b < 0 && si_a > INT_MAX + si_b)) {
    /* Handle error condition */
  } else {
    result = si_a - si_b;
  }

  /* ... */
}

...

Code Block
bgColor#ccccff
langc
#include <limits.h>
 
signed int si_a;
signed int si_b;
signed int result;

void func(void) {
  /* Initialize si_a, si_b and result */

  if (si_a > 0) {  /* si_a is positive */
    if (si_b > 0) {  /* si_a and si_b are positive */
      if (si_a > (INT_MAX / si_b)) {
        /* Handle error condition */
      }
    } else { /* si_a positive, si_b nonpositive */
      if (si_b < (INT_MIN / si_a)) {
        /* Handle error condition */
      }
    } /* si_a positive, si_b nonpositive */
  } else { /* si_a is nonpositive */
    if (si_b > 0) { /* si_a is nonpositive, si_b is positive */
      if (si_a < (INT_MIN / si_b)) {
        /* Handle error condition */
      }
    } else { /* si_a and si_b are nonpositive */
      if ( (si_a != 0) && (si_b < (INT_MAX / si_a))) {
        /* Handle error condition */
      }
    } /* End if si_a and si_b are nonpositive */
  } /* End if si_a is nonpositive */

  result = si_a * si_b;

}

...

Code Block
bgColor#ccccff
langc
#include <limits.h>
#include <stdatomic.h>
 
atomic_int i;
int si_a;

void func(void) {
  /* Initialize si_a, i */
  int si_b = atomic_load(&i);

  if (((si_b>0) && (si_a > (INT_MAX-si_b))) ||
    ||  ((si_b<0) && (si_a < (INT_MIN-si_b)))) {
    /* Handle error condition */
  } else {
    atomic_fetch_add(&i, si_a);
  }

  /* ... */
}

...