Memory leaks in cmd_display_menu, from Huihui Huang.

This commit is contained in:
nicm
2026-02-23 08:50:00 +00:00
parent 50a3b4c777
commit 8c7278b53a
2 changed files with 21 additions and 16 deletions

View File

@@ -295,7 +295,7 @@ cmd_display_menu_exec(struct cmd *self, struct cmdq_item *item)
const char *border_style = args_get(args, 'S'); const char *border_style = args_get(args, 'S');
const char *selected_style = args_get(args, 'H'); const char *selected_style = args_get(args, 'H');
enum box_lines lines = BOX_LINES_DEFAULT; enum box_lines lines = BOX_LINES_DEFAULT;
char *title, *cause; char *title, *cause = NULL;
int flags = 0, starting_choice = 0; int flags = 0, starting_choice = 0;
u_int px, py, i, count = args_count(args); u_int px, py, i, count = args_count(args);
struct options *o = target->s->curw->window->options; struct options *o = target->s->curw->window->options;
@@ -313,8 +313,7 @@ cmd_display_menu_exec(struct cmd *self, struct cmdq_item *item)
&cause); &cause);
if (cause != NULL) { if (cause != NULL) {
cmdq_error(item, "starting choice %s", cause); cmdq_error(item, "starting choice %s", cause);
free(cause); goto fail;
return (CMD_RETURN_ERROR);
} }
} }
} }
@@ -335,8 +334,7 @@ cmd_display_menu_exec(struct cmd *self, struct cmdq_item *item)
if (count - i < 2) { if (count - i < 2) {
cmdq_error(item, "not enough arguments"); cmdq_error(item, "not enough arguments");
menu_free(menu); goto fail;
return (CMD_RETURN_ERROR);
} }
key = args_string(args, i++); key = args_string(args, i++);
@@ -348,17 +346,13 @@ cmd_display_menu_exec(struct cmd *self, struct cmdq_item *item)
} }
if (menu == NULL) { if (menu == NULL) {
cmdq_error(item, "invalid menu arguments"); cmdq_error(item, "invalid menu arguments");
return (CMD_RETURN_ERROR); goto fail;
}
if (menu->count == 0) {
menu_free(menu);
return (CMD_RETURN_NORMAL);
} }
if (menu->count == 0)
goto out;
if (!cmd_display_menu_get_pos(tc, item, args, &px, &py, menu->width + 4, if (!cmd_display_menu_get_pos(tc, item, args, &px, &py, menu->width + 4,
menu->count + 2)) { menu->count + 2))
menu_free(menu); goto out;
return (CMD_RETURN_NORMAL);
}
value = args_get(args, 'b'); value = args_get(args, 'b');
if (value != NULL) { if (value != NULL) {
@@ -367,8 +361,7 @@ cmd_display_menu_exec(struct cmd *self, struct cmdq_item *item)
&cause); &cause);
if (lines == -1) { if (lines == -1) {
cmdq_error(item, "menu-border-lines %s", cause); cmdq_error(item, "menu-border-lines %s", cause);
free(cause); goto fail;
return (CMD_RETURN_ERROR);
} }
} }
@@ -380,6 +373,15 @@ cmd_display_menu_exec(struct cmd *self, struct cmdq_item *item)
style, selected_style, border_style, target, NULL, NULL) != 0) style, selected_style, border_style, target, NULL, NULL) != 0)
return (CMD_RETURN_NORMAL); return (CMD_RETURN_NORMAL);
return (CMD_RETURN_WAIT); return (CMD_RETURN_WAIT);
out:
menu_free(menu);
return (CMD_RETURN_NORMAL);
fail:
free(cause);
menu_free(menu);
return (CMD_RETURN_ERROR);
} }
static enum cmd_retval static enum cmd_retval

3
menu.c
View File

@@ -161,6 +161,9 @@ menu_free(struct menu *menu)
{ {
u_int i; u_int i;
if (menu == NULL)
return;
for (i = 0; i < menu->count; i++) { for (i = 0; i < menu->count; i++) {
free((void *)menu->items[i].name); free((void *)menu->items[i].name);
free((void *)menu->items[i].command); free((void *)menu->items[i].command);