STRONG.TYPE.ASSIGN.RETURN

Assignment strong type with inexplicit return

The 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.ASSIGN.RETURN checker finds function return statements in which a strong type is expected, but not present.

Vulnerability and risk

A 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 example

1 typedef float Speed;
2 
3 Speed getTurtleSpeed()
4 {
5   return 1.0;
6 }

Klocwork flags line 5, indicating that the returned value is not of the expected strong type Speed.

Fixed code example

1 typedef float Speed;
2 
3 Speed getTurtleSpeed()
4 {
5   return (Speed)1.0;
6 }

When the returned value is explicitly cast to the strong type Speed, no report is produced.