Fix realloc misusages

x = realloc(x) construct nullifies x in case of failure and causes a memleak