reset prefix_len = 0
Add SAFE_FREE for char * errstr
Add more errstr pointer check