STRONG.TYPE.JOIN.CONSTComparison of strong type with constantThe STRONG.TYPE family of checkers detects situations in which programmer-enforced strong typing (type-defined abstract types) is broken or ignored, allowing the underlying ANSI type semantics to dominate. The STRONG.TYPE.JOIN.CONST checker looks for an instance in which a strongly typed value is compared with a constant using a binary operator. In this rule, constants can be considered: integral constants, quoted strings, or expressions of the form &v, in which v is a static or automatic variable. Vulnerability and riskA compiler following the ANSI standard won't report a warning for this sort of issue, as it checks only the underlying types, not the surface, or programmer-defined, types. As a result, it's possible that a logic error can occur. Vulnerable code example1 typedef int Weight; 2 3 int main() { 4 Weight w; 5 if (w == 321) ; 6 return 0; 7 } Klocwork flags line 5, indicating that the strongly typed value w is compared with a constant using binary operator ==. Fixed code example1 typedef int Weight; 2 3 int main() { 4 Weight w; 5 if (w == (Weight) 321) ; 6 return 0; 7 } In the fixed code, the comparison is made between two strongly typed Weight values. |