Variables pool->ptr and pool->start point to addresses
that may have been freed if realloc chose the path of
a new base address. So we do the math on these pointers
while they are not dangling, yet.
For a related article:
http://trust-in-soft.com/dangling-pointer-indeterminate/
int blockSize = (int)((unsigned)(pool->end - pool->start)*2U);
size_t bytesToAllocate;
+ // NOTE: Needs to be calculated prior to calling `realloc`
+ // to avoid dangling pointers:
+ const ptrdiff_t offsetInsideBlock = pool->ptr - pool->start;
+
if (blockSize < 0)
return XML_FALSE;
return XML_FALSE;
pool->blocks = temp;
pool->blocks->size = blockSize;
- pool->ptr = pool->blocks->s + (pool->ptr - pool->start);
+ pool->ptr = pool->blocks->s + offsetInsideBlock;
pool->start = pool->blocks->s;
pool->end = pool->start + blockSize;
}