Avoid leaking mallocs on error cases