Fix some bugs reported by scan-build