Fix errors detected by scan-build and enable it in CI
This was meant to fix some obvious leaks but turned out into longer series enabling scan-build in CI and fixing some issues spotted along the way. These are all harmless under normal operation but it's good to have the error paths correct as well.
@arun-mani-j please have a look. I'll update CI images again once !611 (merged) lands.
Edited by Guido Günther