Added memory (re)allocation checks.
 - Guards against memory alloc size overflow.
 - Memory leak on failed reallocation.