Fixed the most obvious Clang static analyzer warnings

Clang finds hundreds of potential issue and this fixes the most obvious ones and which are guaranteed not to be false positives. I ran the unit tests to make sure there were no regressions.