We know that checking return values prevent our software from unexpected states. (You can see CWE definition.) But, we are sometimes sure about the return value. For example:
bool calculateSquareRootReturnFalseIfInputIsNegative(float input, float& output);
float calculateHypotenuse(float a, float b){
float c2 = (a*a) + (b*b);
float c;
calculateSquareRootReturnFalseIfInputIsNegative(c2, c);
return c;
}
The local variable c2
is always positive. So, calculateSquareRootReturnFalseIfInputIsNegative()
always returns true. I shouldn't check its return value.
Finally, Is "check all return values not already known by caller" a valid idiom?
Thanks..
a*a
can overflow