From 755d8f7bcb1bc407bb636ecbc61ef7b64639444f Mon Sep 17 00:00:00 2001 From: AKuHAK <621640+AKuHAK@users.noreply.github.com> Date: Sat, 28 Aug 2021 13:25:33 +0300 Subject: [PATCH] Fixed USBExtreme issue --- src/supportbase.c | 62 ++++++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 33 deletions(-) diff --git a/src/supportbase.c b/src/supportbase.c index 3c4505c01..e9ddff735 100644 --- a/src/supportbase.c +++ b/src/supportbase.c @@ -403,51 +403,47 @@ int sbReadList(base_game_info_t **list, const char *prefix, int *fsize, int *gam snprintf(path, sizeof(path), "%sul.cfg", prefix); fd = openFile(path, O_RDONLY); if (fd >= 0) { - int ulcount; USBExtreme_game_entry_t GameEntry; if (count < 0) count = 0; size = getFileSize(fd); *fsize = size; - ulcount = size / sizeof(USBExtreme_game_entry_t); + count += size / sizeof(USBExtreme_game_entry_t); - if (ulcount > 0) { - if ((*list = (base_game_info_t *)malloc(sizeof(base_game_info_t) * ulcount)) != NULL) { - memset(*list, 0, sizeof(base_game_info_t) * ulcount); + if (count > 0) { + if ((*list = (base_game_info_t *)malloc(sizeof(base_game_info_t) * count)) != NULL) { + memset(*list, 0, sizeof(base_game_info_t) * count); while (size > 0) { + int ulfd = 1; + u8 part; + unsigned int name_checksum; + base_game_info_t *g = &(*list)[id++]; + + // populate game entry in list even if entry corrupted read(fd, &GameEntry, sizeof(USBExtreme_game_entry_t)); size -= sizeof(USBExtreme_game_entry_t); - // populate game entry in list only if it has valid magic - if (!memcmp(GameEntry.magic, "ul.", 3)) { - int ulfd = 1; - u8 part; - unsigned int name_checksum; - base_game_info_t *g = &(*list)[id++]; - count++; - - // to ensure no leaks happen, we copy manually and pad the strings - memcpy(g->name, GameEntry.name, UL_GAME_NAME_MAX); - g->name[UL_GAME_NAME_MAX] = '\0'; - memcpy(g->startup, GameEntry.startup, GAME_STARTUP_MAX); - g->startup[GAME_STARTUP_MAX] = '\0'; - g->extension[0] = '\0'; - g->parts = GameEntry.parts; - g->media = GameEntry.media; - g->format = GAME_FORMAT_USBLD; - g->sizeMB = 0; - name_checksum = USBA_crc32(g->name); - - // calculate total size - for (part = 0; part < g->parts && ulfd >= 0; part++) { - snprintf(path, sizeof(path), "%sul.%08X.%s.%02x", prefix, name_checksum, g->startup, part); - ulfd = openFile(path, O_RDONLY); - if (ulfd >= 0) { - g->sizeMB += (getFileSize(ulfd) >> 20); - close(ulfd); - } + // to ensure no leaks happen, we copy manually and pad the strings + memcpy(g->name, GameEntry.name, UL_GAME_NAME_MAX); + g->name[UL_GAME_NAME_MAX] = '\0'; + memcpy(g->startup, GameEntry.startup, GAME_STARTUP_MAX); + g->startup[GAME_STARTUP_MAX] = '\0'; + g->extension[0] = '\0'; + g->parts = GameEntry.parts; + g->media = GameEntry.media; + g->format = GAME_FORMAT_USBLD; + g->sizeMB = 0; + name_checksum = USBA_crc32(g->name); + + // calculate total size for individual game + for (part = 0; part < g->parts && ulfd >= 0; part++) { + snprintf(path, sizeof(path), "%sul.%08X.%s.%02x", prefix, name_checksum, g->startup, part); + ulfd = openFile(path, O_RDONLY); + if (ulfd >= 0) { + g->sizeMB += (getFileSize(ulfd) >> 20); + close(ulfd); } } }