utils: fix resource leak and bad free

Coverity CID 1005243 and CID 1005244

Change-Id: I29fbc8d82278951064e984eeb08fdb9189136309