Review and Fix Non-initialized Variable Checks
This topic describes how to systematically review the results of a Non-initialized variable check in Polyspace® Code Prover™.
Follow one or more of these steps until you determine a fix for the Non-initialized
variable check. There are multiple ways to fix this check. For a
description of the check and code examples, see Non-initialized 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
On the Results List pane, select the check. On the Result Details pane, obtain further information about the check.
Obtain the following information:
Probable cause of check, if described on the Result Details pane.
In the preceding example, there is an orange Non-initialized variable check on the global variable
globVar
.The software detects that the check is potentially a path-related issue. Therefore, the global variable can be non-initialized only on certain execution paths. For example, you initialized the global variable in an
if
block, but did not initialize it in the correspondingelse
block.Possible fix: Determine along which paths the global variables can be non-initialized.
Value of global variable, if initialized.
In the preceding example, when initialized, the global variable
globVar
has value 0.
Step 2: Determine Root Cause of Check
You can perform the following steps in the Polyspace user interface only.
Right-click the variable and select Go To Definition. Initialize the variable when you define it. If you do not want to initialize during definition, identify a suitable point to initialize the variable before you read it.
If the check is orange, determine why the variable is non-initialized on certain execution paths.
Right-click the variable. Select Show In Variable Access View.
On the Variable Access pane, select each write operation on the variable.
Write operations are indicated with and read operations with .
Determine if the write operation occurs:
Before the read operation containing the orange Non-initialized variable check.
Possible fix: If the write operation occurs after the read operation, see if you intended to perform the operations in reverse order.
In an unreachable code block.
Possible fix: Investigate why the code block is unreachable. See Review and Fix Unreachable Code Checks.
In a code block that is not reached on certain execution paths. For example, the operation occurs in an
if
block in a function. Theif
block is not entered for certain function inputs.Possible fix: Perform a write operation on all the execution paths. In the preceding example, perform the write operation in all branches of the
if ... elseif ... else
statement.
Step 3: 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).
Disabling This Check
You can disable this check in two ways:
You can specify that global variables must be considered as initialized. Polyspace considers global variables to be initialized according to ANSI® C standards. The default values are:
0 for
int
0 for
char
0.0 for
float
For more information, see Ignore default initialization of global variables.
You can disable the check along with other initialization checks. If you disable this check, Polyspace assumes that at declaration, variables have the full range of values allowed by their type. For more information, see
Disable checks for non-initialization (-disable-initialization-checks)
.