简体   繁体   中英

How to interpret Null pointer dereference by Cppcheck?

I have come across a code during review and when i used cpp check it states an null pointer dereference error. I am unable to figure out the reason. Below is the code:

CopyMemory(NULL, dummyMatrixManager.GetConstDataPtr(), dummyMatrixManager.GetNumberOfElements() * sizeof(tFloat)); 

void CopyMemory( tFloat* pDst, const tFloat* pSrc, const tSize nBytes )
{
    // copy data if pointer to this memory is valid
    if (NULL != pDst)
    {
        memcpy(pDst, pSrc, nBytes);
    }
    else
    {
        LOG_ERROR("No Data copied because memory was not properly allocated. Destination pointer was set to NULL.");
    }
}

Thank you

void CopyMemory( tFloat* pDst, const tFloat* pSrc, const tSize nBytes ) has 2 parameters passed as pointers, pDst and pSrc . However, before calling memcpy(pDst, pSrc, nBytes) , just pDst is checked against NULL , not pSrc too.

I am a Cppcheck developer. The warning might be wrong, but it's hard to say when I can't reproduce.

I hope you are using a recent version of Cppcheck (1.84 is latest).

The default text output does not say so much why Cppcheck thinks there is a NULL pointer. Could you try the --template=gcc ? You should be able to see how Cppcheck reached the conclusion that there is a null pointer then.

Best regards, Daniel Marjamäki

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM