utils: Fix double-free in error path

(cherry picked from commit d37f22f12f09ffc0253842d2d410d871e3af5d92)