Fix some bugs detected by Coverity.