2.1: Rpc coverity fixes and plugin cleanup

Cherry picked the two commit already in master plus some code cleanup in rpc plugin by consolidating duplicated code.