Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#FFcccc
langc
void func(signed long s_a;
, signed long s_b;
signed long result;

void func(void) {
  /*signed Initialize s_a and s_b */
  long result = s_a / s_b;

  /* ... */
}

...

Code Block
bgColor#ccccff
langc
#include <limits.h>
 
void func(signed long s_a;
, signed long s_b;
signed long result;

void func(void) {
  /*signed Initialize s_a, s_b and result */

long result;
  if ( (s_b == 0) || ( (s_a == LONG_MIN) && (s_b == -1) ) ) {
    /* Handle error condition */
  } else {
    result = s_a / s_b;
  }

  /* ... */
}

...

Code Block
bgColor#FFcccc
langc
void func(signed long s_a;
, signed long s_b;
signed long result;

void func(void) {
  /* Initialize s_a and s_b */
 signed long result = s_a % s_b;

  /* ... */
}

...

Code Block
bgColor#ccccff
langc
#include <limits.h>
 
void func(signed long s_a;
, signed long s_b;
signed long result;

void func(void) {
  /* Initialize s_a, s_b and result */
signed long result;
  if ( (s_b == 0 ) || ( (s_a == LONG_MIN) && (s_b == -1) ) ) {
    /* Handle error condition */
  } else {
    result = s_a % s_b;
  }
  
  /* ... */
}

Risk Assessment

...

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

Related Guidelines

Bibliography

[ISO/IEC 9899:2011]Subclause 6.5.5, "Multiplicative operators" 
[Seacord 2013]Chapter 5, "Integer Security"
[Warren 2002]Chapter 2, "Basics"

...