diff --git a/mode-tree.c b/mode-tree.c
index f3155fbf..437f0008 100644
--- a/mode-tree.c
+++ b/mode-tree.c
@@ -685,8 +685,10 @@ mode_tree_run_command(struct client *c, struct cmd_find_state *fs,
 	char			*command, *cause;
 
 	command = cmd_template_replace(template, name, 1);
-	if (command == NULL || *command == '\0')
+	if (command == NULL || *command == '\0') {
+		free(command);
 		return;
+	}
 
 	cmdlist = cmd_string_parse(command, NULL, 0, &cause);
 	if (cmdlist == NULL) {