Check for failure of realloc

Check for the (unlikely) failure of realloc and exit if it happens. This prevents a cppcheck warning about a memory leak.