Fix a bunch of issues reported by coverity.