...
Code Block |
---|
|
signed long s_a;
signed long s_b;
signed long result;
void func(void) {
/* Initialize s_a and s_b */
result = s_a / s_b;
/* ... */
} |
Compliant Solution
This compliant solution tests the suspect division operation to guarantee there is no possibility of divide-by-zero errors or signed overflow:
Code Block |
---|
|
#include <limits.h>
signed long s_a;
signed long s_b;
signed long result;
void func(void) {
/* Initialize s_a, and s_b and result*/
if ( (s_b == 0) || ( (s_a == LONG_MIN) && (s_b == -1) ) ) {
/* Handle error condition */
}
else {
result = s_a / s_b;
}
/* ... */
} |
Modulo
The modulo operator provides the remainder when two operands of integer type are divided.
...
Code Block |
---|
|
signed long s_a;
signed long s_b;
signed long result;
void func(void) {
/* Initialize s_a and s_b */
result = s_a % s_b;
/* ... */
} |
Compliant Solution
This compliant solution tests the suspect modulo operation to guarantee there is no possibility of a divide-by-zero error or an overflow error:
Code Block |
---|
|
#include <limits.h>
signed long s_a;
signed long s_b;
signed long result;
void func(void) {
/* Initialize s_a and, s_b and result*/
if ( (s_b == 0 ) || ( (s_a == LONG_MIN) && (s_b == -1) ) ) {
/* Handle error condition */
}
else {
result = s_a % s_b;
}
} |
Risk Assessment
A divide by zero can result in abnormal program termination and denial of service.
...