Edit

Warning C26822

Dereferencing a 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 is null at the point where it's dereferenced, it emits warning C26822. For example, the pointer might be explicitly assigned nullptr, or the dereference might occur on a branch that establishes the pointer is null. You can also enable C26823 to report dereferences where a pointer might be null. This check also supports SAL annotations and gsl::not_null to describe invariants of the code.

Example

void f(int *p) { 
    if (p == nullptr) 
        *p = 42; // warning: C26822
} 

void assign_to_gsl_notnull() { 
    int* p = nullptr; 
    auto q = gsl::make_not_null(p); // C26822 warning 
} 

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; 
}