Clang fixes

These commits fix some bugs detected by clang static analysis.