fixes for part of errors found using clang checker and coverity

"make check" was ok.

full scan reports here:
https://www.dropbox.com/s/7w9zx8mg3d5c8tb/static_analyze_report.zip?dl=0
you can try to solve remaining errors.