...
This noncompliant code example can result in a divide-by-zero error during the division of the signed operands s_a
and s_b.
It can also result in a signed integer overflow error on twos-complement platforms. The On the x86-32 architecture, for example, requires that both conditions result overflow results in a fault, which can easily result in be exploited as a denial-of-service attack.
...
Compliant Solution
This compliant solution tests the suspect division operation to guarantee there is no eliminates the possibility of divide-by-zero errors or signed overflow:
...
The C Standard, 6.5.7 paragraph 4 [ISO/IEC 9899:2011], states
...
In almost every case, an attempt to shift by a negative number of bits or by more bits than exist in the operand indicates a bug (logic error). These issues are covered by INT34-C. Do not shift a negative number of bits or more bits than exist in the operand.
Noncompliant Code Example
This noncompliant code example can result in an unrepresentable value.
...
Compliant Solution
This compliant solution eliminates the possibility of overflow resulting from a left-shift operation:
...