Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Dereferencing a possibly null pointer 'variable' (lifetime.1)
Remarks
Dereferencing a null pointer is a frequent problem in C and C++. The analysis engine includes several checks to deal with such problems. For a comparison, see this blog post. When the analysis engine determines that a pointer might be null at the point where it's dereferenced, it emits warning C26823. Common causes include a possibly null return value, control-flow paths that merge after one path sets the pointer to null, or a call that can modify the pointer through its address. You can enable C26822 to report only dereferences where the pointer is known to be null. This check also supports SAL annotations and gsl::not_null to describe invariants of the code.
Example
void invalidate(int **pp);
void condition_null_dereference_invalidated(int* p)
{
if (p)
return;
invalidate(&p);
// The call above could reset the value of `p`, thus the low confidence warning.
*p = 5; // warning: C26823
}
To solve this warning, make sure there's no null pointer dereference in the code, potentially by adding null checks. In case the code was found to be correct, false positive findings can often be fixed by using gsl::not_null or SAL annotations. There are some examples how to use some of those annotations below:
_Notnull_ int *get_my_ptr();
gsl::not_null<int *> get_my_ptr2();
void local_analysis(int *p) {
_Analysis_assume_(p != nullptr);
*p = 42;
}
void local_analysis2(_In_ int *p) {
int a = *p;
}