Fix some potential bugs found by clang-analyzer

Need to compile as:

scan-build ./configure --prefix=/usr
scan-build make