Review and Fix Return Value Not Initialized Checks
This topic describes how to systematically review the results of a Return value not initialized check in Polyspace® Code Prover™.
Follow one or more of these steps until you determine a fix for the Return
value not initialized check. There are multiple ways to fix this check.
For a description of the check and code examples, see Return value not initialized
.
Sometimes, especially for an orange check, you can determine that the check does not represent a real error but a Polyspace assumption that is not true for your code. If you can use an analysis option to relax the assumption, rerun the verification using that option. Otherwise, you can add a comment and justification in your result or code.
For the general workflow that applies to all checks, see Interpret Code Prover Results in Polyspace Desktop User Interface or Interpret Code Prover Results in Polyspace Access Web Interface (Polyspace Access).
Step 1: Interpret Check Information
Select the check on the Results List pane. On the Result Details pane, view further information about the check.
View the probable cause of check, if mentioned on the Result Details pane.
In the preceding example, the software identifies a stubbed function,
inputRep
, as probable cause.
Possible fix: To avoid the check, constrain the argument or
return value of inputRep
. For instance, specify that
inputRep
returns values in a certain range, for example,
1..10
. For more information, see Code Prover Assumptions About Stubbed Functions.
Step 2: Determine Root Cause of Check
Determine the root cause of the check in the function body. You can perform the following steps in the Polyspace user interface only.
Navigate to the function definition.
Right-click the function call containing the check. Select Go To Definition, if the option exists.
In the function body, check if a
return
statement occurs before the closing brace of the function.If a
return
statement does not exist:On the Search pane, search for the word
return
, or manually scroll through the function body and look forreturn
statements.For each
return
statement, determine if the statement appears in a scope smaller than function scope.For instance, a
return
statement occurs only in one branch of anif-else
statement.
Possible fix: See if you can place the
return
statement at the end of the function body. For instance, replace the following codewithint func(int ch) { switch(ch) { case 1: return 1; break; case 2: return 2; break; } }
For information on how to enforce this practice, seeint func(int ch) { int temp; switch(ch) { case 1: temp = 1; break; case 2: temp = 2; break; } return temp; }
Number of Return Statements
.
Step 3: Look for Common Causes of Check
Look for common causes of the Return value not initialized check.
See if the
return
statements appear inif-else
,for
orwhile
blocks. Identify conditions when the function does not enter the block.For instance, the function might not enter a
while
block for certain function inputs.Possible fix:
See if you can place the
return
statement at the end of the function body.Otherwise, determine how to avoid the condition when the function does not enter the block.
For instance, if a function does not enter a block for certain inputs, see if you must pass different inputs.
See if you have code constructs such as
goto
that interrupt the normal control flow. See if there are conditions whenreturn
statements in your function cannot be reached because of these code constructs.Possible fix:
Avoid
goto
statements. For information on how to enforce this practice, seeNumber of Goto Statements
.Otherwise, determine how to avoid the condition when
return
statements in your function cannot be reached.
Step 4: Trace Check to Polyspace Assumption
See if you can trace the orange check to a Polyspace assumption that occurs earlier in the code. If the assumption does not hold true in your case, add a comment or justification in your result or code. See Address Results in Polyspace User Interface Through Bug Fixes or Justifications or Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access).
For instance, you have a return
statement in branches of an
if-elseif
block but you do not have the final
else
block. The condition you are testing in the
if-elseif
blocks involve variables obtained from an undefined
function. Then:
Polyspace assumes that for certain values of those variables, none of the
if-elseif
blocks are entered.Therefore, it is possible that the
return
statements are not reached.If you know that those values of the variables do not occur, add a comment and justification describing why you did not change your code.
For more information, see Code Prover Analysis Assumptions.
Disabling This Check
You can disable this check. If you disable this check, Polyspace assumes the following about a function return value if the
function is missing return
statements:
If the return value is a non-pointer variable, it has full-range of values allowed by its type.
If the return value is a pointer, it can be
NULL
-valued or point to a memory block at an unknown offset.
For more information, see Disable checks for non-initialization (-disable-initialization-checks)
.