From e561919a52f168a067e282992f5e45dbe9e1131b Mon Sep 17 00:00:00 2001 From: hexkyz Date: Sun, 30 Jun 2019 18:48:16 +0100 Subject: [PATCH] Fix logic in ldr_ro_manager Fix argument type for isdigit/isxdigit --- fusee/fusee-primary/src/lib/vsprintf.c | 8 +++---- fusee/fusee-secondary/src/ips.c | 2 +- sept/sept-secondary/src/lib/vsprintf.c | 22 +++++++++---------- .../source/set_mitm/setsys_settings_items.cpp | 2 +- stratosphere/ams_mitm/source/utils.cpp | 2 +- .../dmnt/source/dmnt_cheat_manager.cpp | 4 ++-- stratosphere/loader/source/ldr_ro_manager.cpp | 3 ++- stratosphere/pm/source/pm_boot2.cpp | 2 +- thermosphere/src/lib/vsprintf.c | 22 +++++++++---------- 9 files changed, 34 insertions(+), 33 deletions(-) diff --git a/fusee/fusee-primary/src/lib/vsprintf.c b/fusee/fusee-primary/src/lib/vsprintf.c index c646f9f90..0dd4c4514 100644 --- a/fusee/fusee-primary/src/lib/vsprintf.c +++ b/fusee/fusee-primary/src/lib/vsprintf.c @@ -1577,10 +1577,10 @@ int vsscanf(const char *buf, const char *fmt, va_list args) digit = *(str + 1); if (!digit - || (base == 16 && !isxdigit(digit)) - || (base == 10 && !isdigit(digit)) - || (base == 8 && (!isdigit(digit) || digit > '7')) - || (base == 0 && !isdigit(digit))) + || (base == 16 && !isxdigit((unsigned char)digit)) + || (base == 10 && !isdigit((unsigned char)digit)) + || (base == 8 && (!isdigit((unsigned char)digit) || digit > '7')) + || (base == 0 && !isdigit((unsigned char)digit))) break; switch (qualifier) { diff --git a/fusee/fusee-secondary/src/ips.c b/fusee/fusee-secondary/src/ips.c index dedd00c67..8d36772f4 100644 --- a/fusee/fusee-secondary/src/ips.c +++ b/fusee/fusee-secondary/src/ips.c @@ -176,7 +176,7 @@ static inline uint8_t hex_nybble_to_u8(const char nybble) { static bool name_matches_hash(const char *name, size_t name_len, const void *hash, size_t hash_size) { /* Validate name is hex build id. */ for (unsigned int i = 0; i < name_len - 4; i++) { - if (isxdigit(name[i]) == 0) { + if (isxdigit((unsigned char)name[i]) == 0) { return false; } } diff --git a/sept/sept-secondary/src/lib/vsprintf.c b/sept/sept-secondary/src/lib/vsprintf.c index d934144c9..7927c83a2 100644 --- a/sept/sept-secondary/src/lib/vsprintf.c +++ b/sept/sept-secondary/src/lib/vsprintf.c @@ -83,7 +83,7 @@ static char *skip_spaces(const char *str) static unsigned int simple_guess_base(const char *cp) { if (cp[0] == '0') { - if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2])) + if (TOLOWER(cp[1]) == 'x' && isxdigit((unsigned char)cp[2])) return 16; else return 8; @@ -108,10 +108,10 @@ unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int bas if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x') cp += 2; - while (isxdigit(*cp)) { + while (isxdigit((unsigned char)*cp)) { unsigned int value; - value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10; + value = isdigit((unsigned char)*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10; if (value >= base) break; result = result * base + value; @@ -167,7 +167,7 @@ int skip_atoi(const char **s) { int i = 0; - while (isdigit(**s)) + while (isdigit((unsigned char)**s)) i = i*10 + *((*s)++) - '0'; return i; @@ -654,7 +654,7 @@ int format_decode(const char *fmt, struct printf_spec *spec) /* get field width */ spec->field_width = -1; - if (isdigit(*fmt)) + if (isdigit((unsigned char)*fmt)) spec->field_width = skip_atoi(&fmt); else if (*fmt == '*') { /* it's the next argument */ @@ -667,7 +667,7 @@ precision: spec->precision = -1; if (*fmt == '.') { ++fmt; - if (isdigit(*fmt)) { + if (isdigit((unsigned char)*fmt)) { spec->precision = skip_atoi(&fmt); if (spec->precision < 0) spec->precision = 0; @@ -1485,7 +1485,7 @@ int vsscanf(const char *buf, const char *fmt, va_list args) /* get field width */ field_width = -1; - if (isdigit(*fmt)) + if (isdigit((unsigned char)*fmt)) field_width = skip_atoi(&fmt); /* get conversion qualifier */ @@ -1577,10 +1577,10 @@ int vsscanf(const char *buf, const char *fmt, va_list args) digit = *(str + 1); if (!digit - || (base == 16 && !isxdigit(digit)) - || (base == 10 && !isdigit(digit)) - || (base == 8 && (!isdigit(digit) || digit > '7')) - || (base == 0 && !isdigit(digit))) + || (base == 16 && !isxdigit((unsigned char)digit)) + || (base == 10 && !isdigit((unsigned char)digit)) + || (base == 8 && (!isdigit((unsigned char)digit) || digit > '7')) + || (base == 0 && !isdigit((unsigned char)digit))) break; switch (qualifier) { diff --git a/stratosphere/ams_mitm/source/set_mitm/setsys_settings_items.cpp b/stratosphere/ams_mitm/source/set_mitm/setsys_settings_items.cpp index 561cc32dc..34a00ce5b 100644 --- a/stratosphere/ams_mitm/source/set_mitm/setsys_settings_items.cpp +++ b/stratosphere/ams_mitm/source/set_mitm/setsys_settings_items.cpp @@ -116,7 +116,7 @@ Result SettingsItemManager::ValidateKey(const char *key) { static bool IsHexadecimal(const char *str) { while (*str) { - if (isxdigit(*str)) { + if (isxdigit((unsigned char)*str)) { str++; } else { return false; diff --git a/stratosphere/ams_mitm/source/utils.cpp b/stratosphere/ams_mitm/source/utils.cpp index 96b38d4a3..46e9fe236 100644 --- a/stratosphere/ams_mitm/source/utils.cpp +++ b/stratosphere/ams_mitm/source/utils.cpp @@ -70,7 +70,7 @@ static FsFile g_emummc_file = {0}; static bool IsHexadecimal(const char *str) { while (*str) { - if (isxdigit(*str)) { + if (isxdigit((unsigned char)*str)) { str++; } else { return false; diff --git a/stratosphere/dmnt/source/dmnt_cheat_manager.cpp b/stratosphere/dmnt/source/dmnt_cheat_manager.cpp index 87d0a69ee..aaa1ee41c 100644 --- a/stratosphere/dmnt/source/dmnt_cheat_manager.cpp +++ b/stratosphere/dmnt/source/dmnt_cheat_manager.cpp @@ -349,7 +349,7 @@ bool DmntCheatManager::ParseCheats(const char *s, size_t len) { /* Skip onwards. */ i = j + 1; - } else if (isxdigit(s[i])) { + } else if (isxdigit((unsigned char)s[i])) { /* Make sure that we have a cheat open. */ if (cur_entry == NULL) { return false; @@ -363,7 +363,7 @@ bool DmntCheatManager::ParseCheats(const char *s, size_t len) { /* We're parsing an instruction, so validate it's 8 hex digits. */ for (size_t j = 1; j < 8; j++) { /* Validate 8 hex chars. */ - if (i + j >= len || !isxdigit(s[i+j])) { + if (i + j >= len || !isxdigit((unsigned char)s[i+j])) { return false; } } diff --git a/stratosphere/loader/source/ldr_ro_manager.cpp b/stratosphere/loader/source/ldr_ro_manager.cpp index c1d9ce65d..10f610ae8 100644 --- a/stratosphere/loader/source/ldr_ro_manager.cpp +++ b/stratosphere/loader/source/ldr_ro_manager.cpp @@ -136,13 +136,14 @@ namespace sts::ldr::ro { /* Nintendo doesn't actually care about successful allocation. */ for (size_t i = 0; i < ModuleCountMax; i++) { ModuleInfo *module = &info->modules[i]; - if (!module->in_use) { + if (module->in_use) { continue; } std::memcpy(module->info.build_id, build_id, sizeof(module->info.build_id)); module->info.base_address = address; module->info.size = size; + module->in_use = true; break; } diff --git a/stratosphere/pm/source/pm_boot2.cpp b/stratosphere/pm/source/pm_boot2.cpp index dfa89f4ad..96ca703b5 100644 --- a/stratosphere/pm/source/pm_boot2.cpp +++ b/stratosphere/pm/source/pm_boot2.cpp @@ -32,7 +32,7 @@ static bool IsHexadecimal(const char *str) { while (*str) { - if (isxdigit(*str)) { + if (isxdigit((unsigned char)*str)) { str++; } else { return false; diff --git a/thermosphere/src/lib/vsprintf.c b/thermosphere/src/lib/vsprintf.c index d934144c9..7927c83a2 100644 --- a/thermosphere/src/lib/vsprintf.c +++ b/thermosphere/src/lib/vsprintf.c @@ -83,7 +83,7 @@ static char *skip_spaces(const char *str) static unsigned int simple_guess_base(const char *cp) { if (cp[0] == '0') { - if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2])) + if (TOLOWER(cp[1]) == 'x' && isxdigit((unsigned char)cp[2])) return 16; else return 8; @@ -108,10 +108,10 @@ unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int bas if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x') cp += 2; - while (isxdigit(*cp)) { + while (isxdigit((unsigned char)*cp)) { unsigned int value; - value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10; + value = isdigit((unsigned char)*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10; if (value >= base) break; result = result * base + value; @@ -167,7 +167,7 @@ int skip_atoi(const char **s) { int i = 0; - while (isdigit(**s)) + while (isdigit((unsigned char)**s)) i = i*10 + *((*s)++) - '0'; return i; @@ -654,7 +654,7 @@ int format_decode(const char *fmt, struct printf_spec *spec) /* get field width */ spec->field_width = -1; - if (isdigit(*fmt)) + if (isdigit((unsigned char)*fmt)) spec->field_width = skip_atoi(&fmt); else if (*fmt == '*') { /* it's the next argument */ @@ -667,7 +667,7 @@ precision: spec->precision = -1; if (*fmt == '.') { ++fmt; - if (isdigit(*fmt)) { + if (isdigit((unsigned char)*fmt)) { spec->precision = skip_atoi(&fmt); if (spec->precision < 0) spec->precision = 0; @@ -1485,7 +1485,7 @@ int vsscanf(const char *buf, const char *fmt, va_list args) /* get field width */ field_width = -1; - if (isdigit(*fmt)) + if (isdigit((unsigned char)*fmt)) field_width = skip_atoi(&fmt); /* get conversion qualifier */ @@ -1577,10 +1577,10 @@ int vsscanf(const char *buf, const char *fmt, va_list args) digit = *(str + 1); if (!digit - || (base == 16 && !isxdigit(digit)) - || (base == 10 && !isdigit(digit)) - || (base == 8 && (!isdigit(digit) || digit > '7')) - || (base == 0 && !isdigit(digit))) + || (base == 16 && !isxdigit((unsigned char)digit)) + || (base == 10 && !isdigit((unsigned char)digit)) + || (base == 8 && (!isdigit((unsigned char)digit) || digit > '7')) + || (base == 0 && !isdigit((unsigned char)digit))) break; switch (qualifier) {