Free allocated errmsg, switch to calloc for automatic zero-ing