Yeee-harrr!
I don't know what that code is intended to accomplish, but I sure hope the coder was aware that what ends up in dispBuf is not delimited by zero bytes. I also don't see freeSize being adjusted after the strncat / strcat calls, but that might be happening elsewhere. What's really juicy is the realloc() part though:
Code: Select all
if( (dispBuf = (char*)realloc(dispBuf, totalBufSize+BufSize) ) == NULL )
{ //realloc has failed, make sure no overwrite into stack!
strncat (dispBuf, node_p, freeSize);
}
If you want to handle out-of-memory situations correctly, never allocate the return code of realloc() to what you pass to it as first parameter: realloc() returns NULL on out-of-memory, but its first parameter is still a pointer to very valid allocated memory you could never free() again if you overwrote the pointer with NULL.
But this code will never get that far because strncat() will copy freeSize bytes of node_p to NULL, and you know what that means.
Then again, most (paging) OS's die violently on out-of-memory without any malloc() ever returning NULL, anyway. (malloc() always succeeds because the pages it allocates aren't actually mapped until much later when they're actually used, which means the PF handler dies long after the actual malloc() succeeds, a weakness shared by many operating systems AFAIK.)
Code: Select all
else
{
strncat (dispBuf, node_p, BufSize);
totalBufSize = totalBufSize+BufSize;
}
This merely truncates node_p to BufSize bytes (if sizeof( node_p ) + 1 > BufSize), which might or might not be what you want. Oh, and I prefer the += operator in cases like this, but that's just code candy.