...
Code Block | ||
---|---|---|
| ||
signed long int sl = LONG_MAX;
signed char sc;
if ( (sl < SCHAR_MIN) || (sl > SCHAR_MAX) ) {
/* handle error condition */
}
else {
sc = (signed char)sl; /* use cast to eliminate warning */
}
|
Conversions from signed types with greater precision to signed types with lesser precision require both the upper and lower bounds to be checked.
...
Fortify SCA Version 5.0 with CERT C Rule Pack is able to detect violations of this rule.
Compass/ROSE is able to can detect violations of this rule. However, but may throw false warnings when may be raised if limits.h is included.
The Coverity Prevent NEGATIVE_RETURNS and REVERSE_NEGATIVE checkers both can find violations of this rule. The NEGATIVE_RETURNS checker can find array accesses, loop bounds, and other expressions that may contain dangerous implied integer conversions that would result in unexpected behavior. The REVERSE_NEGATIVE checker can find instances where a negativity check occurs after the negative value has been used for something else. Coverity Prevent cannot discover all violations of this rule, so further verification is necessary.
...