The Problem is the following:
There is the call of a constructor, where a pointer is an argument
m_pszBuf= new char[260];
//Still valid , to prove i'm printing the address
std::cout <<"Address "<< (void*)m_pszBuf <<std::endl;
device = new Device(m_pszBuf);
Device::Device(char* _ptr){
strcpy(dest,_ptr);
}
Interesting is, before the call of the constructor, the pointer is still valid and has an address and value, but as soon as it entered the ctor, it becomes a bad pointer (0x0000005c). In addition, it is working in the debug mode but not in the release mode.
You initialize m_pszBuf
like this:
m_pszBuf = new char[260];
then you call Device
constructor like this:
device = new Device(m_pszBuf);
Inside Device
constructor, there is a strcpy
call from m_pszBuf
source:
Device::Device(char* _ptr) // _ptr == m_pszBuf
{
strcpy(dest, _ptr);
}
But if m_pszBuf
is not NUL-terminated, strcpy
doesn't stop at the end of the allocated buffer, and it can copy garbage from out-of-bounds memory, and you can overrun the dest
buffer.
So, before passing m_pszBuf
to Device
constructor, make sure that it is NUL-terminated and that strcpy
destination pointer is big enough.
Note: This analysis is based just on the code snippet you showed. (I don't know if in your actual code that you omitted to show there are other problems.)
Great input but i have solved it with something else. I had some Header files "out of sync" which were used for a library. i just needed to update them. Strange how this affected something else.
User contributions licensed under CC BY-SA 3.0