Example 1: Uninitialized Variables and NULL Pointers
The pft-example2.cpp file included in PREfast for Drivers (in the \tools\pfd\samples subdirectory of the WDK) includes a function named test that triggers Warning 6011 - Dereferencing NULL pointer <pointer>. The following screen shot shows warning 6011 for the test function in pft-example2.cpp.
To reproduce this example, open the Windows 2003 Free Build Environment window, navigate to the \tools\pfd\samples subdirectory of the WDK, and type the following command:
prefast build -cZ
Type prefast view to open the PREfast for Drivers Defect Log. Click the title of the Warnings column to sort the warnings by warning number. Then double-click the first instance of Warning 6011 (# 31).
In this example, PREfast for Drivers has detected that a pointer variable, p, is declared, but never initialized. The error occurs because the integer variable, a, is not initialized. Because a is not initialized, the conditional on line 17 fails. As a result, line 19, in which p has been initialized, is not executed. Although p is tested on line 26, no code handles the case in which the test fails, so execution continues on line 31, which dereferences p and triggers a warning that p could be NULL.
Among the many fixes for this error, the obvious ones would be to initialize a and p when they are declared and to verify that p is initialized before dereferencing it.
Send comments about this topic to Microsoft
Build date: 5/3/2011