# V522. Possible null pointer dereference.

- malloc, realloc
- Additional Settings

The analyzer detected a fragment of code that might cause using a null pointer.

Let's study several examples the analyzer generates the V522 diagnostic message for:

    if (pointer != 0 || pointer->m_a) { ... }
    if (pointer == 0 && pointer->x()) { ... }
    if (array == 0 && array[3]) { ... }
    if (!pointer && pointer->x()) { ... }

In all the conditions, there is a logical error that leads to dereferencing of the null pointer. The error may be introduced into the code during code refactoring or through a misprint.

Correct versions:

    if (pointer == 0 || pointer->m_a) { ... }
    if (pointer != 0 && pointer->x()) { ... }
    if (array != 0 && array[3]) { ... }
    if (pointer && pointer->x()) { ... }

These are simple cases, of course. In practice, operations of pointer check and pointer use may be located in different places. If the analyzer generates the V522 warning, study the code above and try to understand why the pointer might be a null pointer.

Here is a code sample where pointer check and pointer use are in different strings

    if (ptag == NULL) {
      SysPrintf("SPR1 Tag BUSERR\n");
      psHu32(DMAC_STAT)|= 1<<15;
      spr1->chcr = ( spr1->chcr & 0xFFFF ) |
                   ( (*ptag) & 0xFFFF0000 );
      return;
    }

The analyzer will warn you about the danger in the "( (*ptag) & 0xFFFF0000 )" string. It's either an incorrectly written condition here or there should be a different variable instead of 'ptag'.

Sometimes programmers deliberately use null pointer dereferencing for the testing purpose. For example, analyzer will produce the warning for those places that contain this macro:

    /// This generate a coredump when we need a
    /// method to be compiled but not usabled.
    #define elxFIXME { char * p=0; *p=0; }

Extraneous warnings can be turned off by using the "//-V522" comment in those strings that contain the 'elxFIXME' macro. Or, as an alternative, you can write a comment of a special kind beside the macro:

    //-V:elxFIXME:522

The comment can be written both before and after the macro - it doesn't matter. To learn more about methods of suppressing false positives, follow here.

## malloc, realloc

Programmers often don't preliminarily check the pointer returned by the 'malloc' or similar functions. This omission often results in a warning. Some programmers believe that it is not necessary to check the pointer. If a programmer gets a memory allocation error, the program is no longer functional anyway. So it is an acceptable scenario when a program crashes due to the null pointer.

However, everything is much more complicated and dangerous than it may seem at first glance. We suggest reading the article: "Why it is important to check what the malloc function returned".

If you still do not plan to check such pointers, keep reading to find out about the specialized analyzer configuration.

## Additional Settings

This diagnostic relies on information about whether a particular pointer could be null. In some cases, this information is retrieved from the table of annotated functions, which is stored inside the analyzer itself.

'malloc' is one of such functions. Since it can return 'NULL', using the pointer returned by it without a prior check may result in null pointer dereferencing.

Sometimes our users wish to change the analyzer's behavior and make it think that 'malloc' cannot return 'NULL'. For example, to do that, they use the system libraries, where 'out of memory' errors are handled in a specific way.

They may also want to tell the analyzer that a certain function can return a null pointer.

In that case, you can use the additional settings, described in the section "How to tell the analyzer that a function can or cannot return nullptr".

This diagnostic is classified as:

CWE-476 CWE-690 CERT-EXP34-C CERT-MEM52-CPP

You can look at examples of errors detected by the V522 diagnostic.