Fix minor buffer overflow reported by Coverity.

CID:		1006781
Fix memory leak on error, reported by Coverity.

CID:		1007773
Fix couple off-by-one range check errors, reported by Coverity.

CID:		1007837