🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Untitled

posted in DruinkJournal
Published February 27, 2007
Advertisement
You know you're in for trouble, when your memory manager corrupts memory [sad]

To be honest, I thought my stack walking code worked too well. I had a lovely off-by-one error in the following code:
unsigned char byBuffer[sizeof(IMAGEHLP_SYMBOL64) + Allocation::cnBufferSize];IMAGEHLP_SYMBOL64* pSymbol = (IMAGEHLP_SYMBOL64*)byBuffer;DWORD64 dwDisplacement;memset(pSymbol, 0, sizeof(IMAGEHLP_SYMBOL64) + Allocation::cnBufferSize);pSymbol->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL64);pSymbol->MaxNameLength = Allocation::cnBufferSize;if(!SymGetSymFromAddr64(GetCurrentProcess(), theStackFrame.AddrPC.Offset, &dwDisplacement, pSymbol))	strcpy(pAllocation->szFunc, "??");else	strcpy(pAllocation->szFunc, pSymbol->Name);
Since if SymGetSymFromAddr64() runs out of space, it doesn't NULL terminate the shitting string. It just so happens that the next byte in memory was NULL, which was causing strcpy() to copy 257 bytes (256+null) into a buffer big enough for 255+null. Wonderful.
On the plus side, my memory manager detected the corruption (I got an assertion saying that a memory sentinal was damaged when I ran this code: {std::map m;}).

In other news, my string hashing "ID" class is working, and has a debug helper doohicky for detecting collisions. Hooraj!
Previous Entry Untitled
Next Entry Untitled
0 likes 3 comments

Comments

rick_appleton
Why aren't you using strncpy? That would have avoided that problem.
February 28, 2007 02:59 AM
Evil Steve
Quote: Original post by rick_appleton
Why aren't you using strncpy? That would have avoided that problem.
Because I thought that the buffer would always be <= 256 bytes long, since I passed that to the function. Oh well, I'll know in future.
February 28, 2007 03:34 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement
Advertisement