Fix various issues detected by Coverity.

Please avoid changing things you don't have to.