fixed various warnings and obvious errors reported by clang compiler and the coverity tool.
Merge pull request #3067 from vpisarev:minor_fixes2