Added memory (re)allocation checks.
 - Guards against memory alloc size overflow.
 - Memory leak on failed reallocation.
Merge pull request #86 from MikeAthene/master

More memory allocation checks