Coverity fixes

Hi, 

Please reviews these patches which add checks for return values. Thanks,

Xing