Update mongoose.c

Proposed fixes from security analysis.
- unverified input issues
- pthread_attr_destroy missing
- allocation NULL checks
I tried to maintain the same coding style, hopefully you accept the change request :)