Clang scan build

Fix some bugs/warnings found by clang's scan-build tool