From: kub Date: Wed, 15 Nov 2023 20:26:05 +0000 (+0000) Subject: fix crash if file list is empty in romsel X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=79ef6fa7d655cbc84bed485983c6fbed2e34ba4a;p=libpicofe.git fix crash if file list is empty in romsel --- 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)) {