Fix issues found by Coverity.

Most of the problems found were resource leaks in error paths, some NULL
pointer dereferences that do not happen in practice, and a few other issues.
They have all been fixed now anyway.
Fix warnings found by GCC 4.9.

Too many arguments for format string in a few error messages.