cmd_exec: fix memleaks, fix crash on missing command

Caught with clang static analyzer.