Reputation:
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
Upvotes: 0
Views: 590
Reputation: 3037
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
Upvotes: 1
Reputation: 140
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.
Upvotes: 3