• Advertisement
Sign in to follow this  
  • entries
    557
  • comments
    1237
  • views
    422720

Untitled

Sign in to follow this  

59 views

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!
Sign in to follow this  


3 Comments


Recommended Comments

Guest Anonymous Poster

Posted

How do you do it? How do you make your journal so dull?

Share this comment


Link to comment
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.

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement