Review and Fix Non-initialized Local Variable Checks
This topic describes how to systematically review the results of a Non-initialized local variable check in Polyspace® Code Prover™.
Follow one or more of these steps until you determine a fix for the Non-initialized
local variable check. There are multiple ways to fix this check. For a
description of the check and code examples, see Non-initialized local variable
.
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
Place your cursor on the variable on which the Non-initialized local variable error appears.
Obtain the probable root cause for the variable being non-initialized, if indicated in the tooltip.
In the preceding example, the software identifies a stubbed
function, initialize
, as probable cause.
Possible fix: To avoid the check, you can specify that
initialize
writes to its arguments. For more information, see
Code Prover Assumptions About Stubbed Functions.
Step 2: Determine Root Cause of Check
You can perform the following steps in the Polyspace user interface only.
Search for the variable definition. See if you initialize the variable when you define it.
Right-click the variable and select Go To Definition, if the option exists.
If you do not want to initialize the variable during definition, browse through all instances of the variable. Determine if you initialize the variable in any of those instances.
Do one of the following:
On the Source pane, double-click the variable.
Previous instances of the variable are highlighted. Scroll up to find them.
On the Source pane, right-click the variable. Select Search For All References.
Select the previous instances on the Search pane.
Possible fix: If you do not initialize the variable, identify an instance where you can initialize it.
If you find an instance where you initialize the variable, determine if you perform the initialization in the scope where the Non-initialized local variable error appears.
For instance, you initialize the variable only in some branches of an
if ... elseif ... else
statement. If you use the variable outside the statement, the variable can be non-initialized.Possible fix:
Perform the initialization in the same scope where you use it.
In the preceding example, perform the initialization outside the
if ... elseif ... else
statement.Perform the initialization in a block with smaller scope but make sure that the block always executes.
In the preceding example, perform the initialization in all branches of the
if ... elseif ... else
statement. Make sure that one branch of the statement always executes.
Step 3: Look for Common Causes of Check
Look for common causes of the Non-initialized local variable check.
See if you pass the variable to another function by reference or pointers before using it. Determine if you initialize the variable in the function body.
To navigate to the function body, right-click the function and select Go To Definition, if the option exists.
Determine if you initialize the variable in code that is not reachable.
For instance, you initialize the variable in code that follows a
break
orreturn
statement.Possible fix: Investigate the unreachable code. For more information, see Review and Fix Unreachable Code Checks.
Determine if you initialize the variable in code that can be bypassed during execution.
For instance, you initialize the variable in a loop inside a function. However, for certain function arguments, the loop does not execute.
Possible fix:
Initialize the variable during declaration.
Investigate when the code can be bypassed. Determine if you can avoid bypassing of the code.
If the variable is an array, determine if you initialize all elements of the array.
If the variable is a structured variable, determine if you initialize all fields of the structure.
If you do not initialize a certain field of the structure, see if the field is unused.
Possible fix: Initialize a field of the structure if you use the field in your code.
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 pass a variable to a function by pointer or reference. You intend to initialize the variable in the function body, but you do not provide the function body during verification. Then:
Polyspace assumes that the function might not initialize the variable.
If you use the variable following the function call, Polyspace considers that the variable can be non-initialized. It produces an orange Non-initialized local variable check on the variable.
For more information, see Code Prover Analysis Assumptions.
Note
Before justifying an orange check, consider carefully whether you can improve your coding design.
Disabling This Check
You can disable this check. If you disable this check, Polyspace assumes that at declaration, variables have full-range of values
allowed by their type. For more information, see Disable checks for non-initialization (-disable-initialization-checks)
.