From 8f588fa8bcf9cb944e3d21d152d05ec1b1c40dd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A1lint=20Kov=C3=A1cs?= Date: Sat, 6 Jul 2019 04:35:41 +0200 Subject: [PATCH] Fix crashes when opening extensionless files. (#503) * Set extension to an empty string when there's none --- arm9/source/filesys/filetype.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/arm9/source/filesys/filetype.c b/arm9/source/filesys/filetype.c index e0d87e1..f52a0d6 100644 --- a/arm9/source/filesys/filetype.c +++ b/arm9/source/filesys/filetype.c @@ -32,7 +32,11 @@ u64 IdentifyFileType(const char* path) { if (!fname) return 0; if (strncmp(fname, "._", 2) == 0) return 0; - if (ext) ext++; + if (ext) { + ext++; + } else { + ext = ""; + } if (FileGetData(path, header, 0x200, 0) < min(0x200, fsize)) return 0; if (!fsize) return 0; @@ -128,7 +132,7 @@ u64 IdentifyFileType(const char* path) { } else if ((strncasecmp(ext, "png", 4) == 0) && (fsize > sizeof(png_magic)) && (memcmp(data, png_magic, sizeof(png_magic)) == 0)) { return GFX_PNG; - } else if (ext && ((strncasecmp(ext, "cdn", 4) == 0) || (strncasecmp(ext, "nus", 4) == 0))) { + } else if ((strncasecmp(ext, "cdn", 4) == 0) || (strncasecmp(ext, "nus", 4) == 0)) { char path_cetk[256]; char* ext_cetk = path_cetk + (ext - path); strncpy(path_cetk, path, 256); @@ -146,7 +150,7 @@ u64 IdentifyFileType(const char* path) { return BIN_LEGKEY; // legacy key file } else if (ValidateText((char*) data, (fsize > 0x200) ? 0x200 : fsize)) { u64 type = 0; - if ((fsize <= SCRIPT_MAX_SIZE) && ext && (strncasecmp(ext, SCRIPT_EXT, strnlen(SCRIPT_EXT, 16) + 1) == 0)) + if ((fsize <= SCRIPT_MAX_SIZE) && (strncasecmp(ext, SCRIPT_EXT, strnlen(SCRIPT_EXT, 16) + 1) == 0)) type |= TXT_SCRIPT; // should be a script (which is also generic text) if (fsize < STD_BUFFER_SIZE) type |= TXT_GENERIC; return type;