Avoid using memory after we free it

Without setting errmsg to NULL here, we'll try to print this freed
value again below if another error doesn't intercede.