Warnings fixes

This includes a few actual bug fixes, combined with lots of warnings fixes, based on analysis from the cppcheck static testing tool.

Nothing here is high priority, and merging it should probably wait until after our next official release; I'm just putting it out there to allow people to run it through test suites first.  I may do another pass with some other tools; if so then I'll branch off of this branch and do a second pull request from that.