Fix bugs reported by scan-build