From 79ef6fa7d655cbc84bed485983c6fbed2e34ba4a Mon Sep 17 00:00:00 2001 From: kub Date: Wed, 15 Nov 2023 20:26:05 +0000 Subject: [PATCH] fix crash if file list is empty in romsel --- menu.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/menu.c b/menu.c index 96b1ea2..ae61038 100644 --- a/menu.c +++ b/menu.c @@ -1038,16 +1038,17 @@ rescan: filter = scandir_filter; n = scandir(curr_path, &namelist, filter, (void *)scandir_cmp); - if (n < 0) { + if (n < 0 || !namelist) { lprintf("menu_loop_romsel failed, dir: %s\n", curr_path); // try data root plat_get_data_dir(curr_path, len); n = scandir(curr_path, &namelist, filter, (void *)scandir_cmp); - if (n < 0) { + if (n < 0 || !namelist) { // oops, we failed lprintf("menu_loop_romsel failed, dir: %s\n", curr_path); - return NULL; + namelist = malloc(sizeof(*namelist)); + namelist[0] = calloc(1, sizeof(**namelist)); } } @@ -1108,12 +1109,13 @@ rescan: namelist[sel]->d_name); goto rescan; } - if (inp & PBTN_UP ) { sel--; if (sel < 0) sel = n-1; } + int last = n ? n-1 : 0; + if (inp & PBTN_UP ) { sel--; if (sel < 0) sel = last; } else if (inp & PBTN_DOWN) { sel++; if (sel > n-1) sel = 0; } else if (inp & PBTN_LEFT) { sel-=10; if (sel < 0) sel = 0; } - else if (inp & PBTN_RIGHT) { sel+=10; if (sel > n-1) sel = n-1; } + else if (inp & PBTN_RIGHT) { sel+=10; if (sel > n-1) sel = last; } else if (inp & PBTN_L) { sel-=24; if (sel < 0) sel = 0; } - else if (inp & PBTN_R) { sel+=24; if (sel > n-1) sel = n-1; } + else if (inp & PBTN_R) { sel+=24; if (sel > n-1) sel = last; } else if ((inp & PBTN_MOK) || (inp & (PBTN_MENU|PBTN_MA2)) == (PBTN_MENU|PBTN_MA2)) { -- 2.39.2