Fix a double memory free and a memory corruption (LIST_REMOVE() on an uninserted node) in some error handling paths.