fix a few bugs found by clang-analyzer