Realloc might leak memory on failure

When realloc fails the original pointer should be preserved, because if will not have been freed.