candidate fix for use after free and buffer overflow in command handling code