Backport some more coverity fixes from master.