Skip to content

Commit 1f035a5

Browse files
committed
kconfig: nconf: fix core dump when searching in empty menu
The following code in get_mext_match(): index = (index + items_num) % items_num; ... makes the program crash when items_num is zero (that is, the menu is empty). A menu can be empty when all the options in it are hidden by unmet 'depends on'. For example, menu "This menu will be empty" config FOO bool "foo" depends on BROKEN endmenu If you visit this menu and press a '/' key and then another key, nconf crashes with: Floating point exception (core dumped) When the number of items is zero, it does not make sense to search in the menu. In this case, current_item() returns NULL, and item_index() ERR, but get_mext_match() does not check it. Let's make get_mext_match() just return if the menu is empty. While I am here, change items_num from 'int' to 'unsigned int' because it should never become negative. Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
1 parent bffbf6e commit 1f035a5

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

scripts/kconfig/nconf.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ static int mwin_max_cols;
268268
static MENU *curses_menu;
269269
static ITEM *curses_menu_items[MAX_MENU_ITEMS];
270270
static struct mitem k_menu_items[MAX_MENU_ITEMS];
271-
static int items_num;
271+
static unsigned int items_num;
272272
static int global_exit;
273273
/* the currently selected button */
274274
static const char *current_instructions = menu_instructions;
@@ -496,8 +496,12 @@ typedef enum {MATCH_TINKER_PATTERN_UP, MATCH_TINKER_PATTERN_DOWN,
496496
/* return the index of the matched item, or -1 if no such item exists */
497497
static int get_mext_match(const char *match_str, match_f flag)
498498
{
499-
int match_start = item_index(current_item(curses_menu));
500-
int index;
499+
int match_start, index;
500+
501+
/* Do not search if the menu is empty (i.e. items_num == 0) */
502+
match_start = item_index(current_item(curses_menu));
503+
if (match_start == ERR)
504+
return -1;
501505

502506
if (flag == FIND_NEXT_MATCH_DOWN)
503507
++match_start;

0 commit comments

Comments
 (0)