...
This code can result in a divide-by-zero error during the division of the signed operands sl1
num1
and sl2
num2
.
Code Block | ||
---|---|---|
| ||
long sl1num1, sl2num2, result; /* Initialize sl1num1 and sl2num2 */ result = sl1num1 / sl2num2; |
Compliant Solution
This compliant solution tests the suspect division operation to guarantee there is no possibility of divide-by-zero errors.
Code Block | ||
---|---|---|
| ||
long sl1num1, sl2num2, result; /* Initialize sl1num1 and sl2num2 */ if ((sl2num2 == 0)) { //* handle error condition */ } else { result = sl1num1 / sl2num2; } |
Modulo
The %
operator provides the remainder when two operands of integer type are divided.
...
This code can result in a divide-by-zero error during the remainder operation on the signed operands sl1
num1
and sl2
num2
.
Code Block | ||
---|---|---|
| ||
long sl1num1, sl2num2, result; /* Initialize sl1num1 and sl2num2 */ result = sl1num1 % sl2num2; |
Compliant Solution
This compliant solution tests the suspect remainder operation to guarantee there is no possibility of a divide-by-zero error.
Code Block | ||
---|---|---|
| ||
long sl1num1, sl2num2, result; /* Initialize sl1num1 and sl2num2 */ if ((sl2num2 == 0)) { /*/ handle error condition */ } else { result = sl1num1 % sl2num2; } |
Risk Assessment
A divide-by-zero can result in abnormal program termination and denial of service.
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="3298b42043d81487-56cdc7a3-45c44174-8ccbbc4e-72a8bd1865304b78d701e3d4"><ac:plain-text-body><![CDATA[ | [[ISO/IEC 9899:1999 | AA. Bibliography#ISO/IEC 9899-1999]] | Section 6.5.5, "Multiplicative operators" | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="4cee3048f8af12d9-7e6c54fb-4c85483e-85debc47-b04b36a1c1108f0e985e56b0"><ac:plain-text-body><![CDATA[ | [[Seacord 05 | AA. Bibliography#Seacord 05]] | Chapter 5, "Integers" | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="2e4967f05e7e2f9d-e3c24a54-41f84dda-8b7fa54b-4eec034c3f10f5f0ac07f8c0"><ac:plain-text-body><![CDATA[ | [[Warren 02 | AA. Bibliography#Warren 02]] | Chapter 2, "Basics" | ]]></ac:plain-text-body></ac:structured-macro> |
...