Main Content

Review and Fix Null This-pointer Calling Method Checks

This topic describes how to systematically review the results of a Null this-pointer calling method check in Polyspace® Code Prover™.

Follow one or more of these steps until you determine a fix for the Null this-pointer calling method check. For a description of the check and code examples, see Null this-pointer calling method.

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. The Result Details pane displays further information about the check.

You can see:

  • The immediate cause of the check.

    In this example, the pointer used to call a method addNewClient can be NULL.

  • The probable root cause of the check, if indicated.

    In this example, the check can be related to a stubbed function returnPointer.

Step 2: Determine Root Cause of Check

Find an execution path where the pointer is either assigned the value NULL or assigned values from an undefined function or unknown function inputs. In the latter case, the software assumes that the pointer can be NULL.

Select the check on the Results List pane.

  • If the Result Details pane shows the sequence of instructions that lead to the check, select each instruction and trace back to the root cause.

  • If the Result Details pane shows the line number of probable cause for the check, in the Polyspace user interface, right-click the Source pane. Select Go To Line.

  • If the Result Details pane does not lead you to the root cause, using the Source pane in the Polyspace user interface, find how the pointer used to call the method can be NULL.

    1. Right-click the pointer and select Search For All References.

    2. Find each previous instance where the pointer is assigned an address.

    3. For each instance, on the Source pane, place your cursor on the pointer. The tooltip indicates whether the pointer can be NULL.

      Possible fix: If the pointer can be NULL, place a check for NULL immediately after the assignment.

      if(ptr==NULL)
        /* Error handling*/
      else {
        .
        .
      	}
    4. If the pointer is not NULL, see if the assignment occurs only in a branch of a conditional statement. Investigate when that branch does not execute.

      Possible fix: Assign a valid address to the pointer in all branches of the conditional statement.