SA0124: Dereference access in initializations

Function

Determines all code positions where dereferenced pointers are used in the declaration part of POUs.

Reason

Pointers and references should not be used for initialization because this can lead to access violations at runtime if the pointer has not been initialized.

Importance

Medium

Samples:

FUNCTION_BLOCK FB_Test
VAR_INPUT
    pStruct    : POINTER TO ST_Test;
    refStruct  : REFERENCE TO ST_Test;
END_VAR
VAR
    bPointer   : BOOL := pStruct^.bTest;  // => SA0124: Dereference access in initialization
    bRef       : BOOL := refStruct.bTest; // => SA0125: Reference used in initialization
END_VAR
bPointer := pStruct^.bTest;               // => SA0039: Possible null pointer dereference 'pStruct^'
bRef     := refStruct.bTest;              // => SA0145: Possible use of not initialized reference 'refStruct'
 
IF pStruct <> 0 THEN
    bPointer := pStruct^.bTest;           // no error SA0039 as the pointer is checked for unequal 0
END_IF

IF __ISVALIDREF(refStruct) THEN
    bRef     := refStruct.bTest;          // no error SA0145 as the reference is checked via __ISVALIDREF
END_IF

Overview of the rules on "dereferencing"

Pointer

Dereferencing of pointers in the declaration part

SA0124: Dereference access in initializations

Possible null pointer dereferences in the implementation part

SA0039: Possible null pointer dereferences

References

Use of references in the declaration part

SA0125: References in initializations

Possible use of not initialized reference in the implementation part

SA0145: Possible use of not initialized references

Interfaces

Possible use of not initialized interface in the implementation part

SA0046: Possible use of not initialized interfaces