Coverity inspired security fixes from Nelson