From a070ad7c1dd5782d454eddaadae9cd0140c51f40 Mon Sep 17 00:00:00 2001 From: Pablo Curiel Date: Thu, 12 Dec 2024 11:00:23 +0100 Subject: [PATCH] utils: fix memchr usage in char repl / escaping Other changes include: * devoptab: minor codestyle changes. * gamecard: update gamecardGetCertificate() to reflect latest libnx changes. --- include/core/devoptab/nxdt_devoptab.h | 6 +++--- include/core/nxdt_utils.h | 6 +++--- source/core/devoptab/fat_dev.c | 4 ++-- source/core/gamecard.c | 5 +++-- source/core/nxdt_utils.c | 11 ++++++----- 5 files changed, 17 insertions(+), 15 deletions(-) diff --git a/include/core/devoptab/nxdt_devoptab.h b/include/core/devoptab/nxdt_devoptab.h index 034a1ce..0ffc5c3 100644 --- a/include/core/devoptab/nxdt_devoptab.h +++ b/include/core/devoptab/nxdt_devoptab.h @@ -36,14 +36,14 @@ extern "C" { #define DEVOPTAB_MOUNT_NAME_LENGTH 32 // Including NULL terminator. #define DEVOPTAB_INIT_ERROR_STATE r->_errno = 0 +#define DEVOPTAB_SET_ERROR(x) r->_errno = (x) +#define DEVOPTAB_IS_ERROR_SET (r->_errno != 0) + #define DEVOPTAB_DECL_DEV_CTX DevoptabDeviceContext *dev_ctx = (DevoptabDeviceContext*)r->deviceData #define DEVOPTAB_DECL_FS_CTX(type) type *fs_ctx = (type*)dev_ctx->fs_ctx #define DEVOPTAB_DECL_FILE_STATE(type) type *file = (type*)fd #define DEVOPTAB_DECL_DIR_STATE(type) type *dir = (type*)dirState->dirStruct -#define DEVOPTAB_SET_ERROR(x) r->_errno = (x) -#define DEVOPTAB_IS_ERROR_SET (r->_errno != 0) - #define DEVOPTAB_EXIT goto end #define DEVOPTAB_SET_ERROR_AND_EXIT(x) \ do { \ diff --git a/include/core/nxdt_utils.h b/include/core/nxdt_utils.h index 28ad81b..3fe65bd 100644 --- a/include/core/nxdt_utils.h +++ b/include/core/nxdt_utils.h @@ -133,9 +133,9 @@ __attribute__((format(printf, 3, 4))) bool utilsAppendFormattedStringToBuffer(ch /// Furthermore, if multiple, consecutive illegal characters are found, they will all get replaced by a single underscore. void utilsReplaceIllegalCharacters(char *str, bool ascii_only); -/// Returns a pointer to a dynamically allocated copy of the provided string with all required characters escaped using another specific character. -/// 'chars_to_escape' must represent a NULL-terminated character string with all characters that need to be escaped. -/// Furthermore, 'escape_char' must represent an ASCII character within the [0x20,0x7E] range, and it must also not be part of 'chars_to_escape'. +/// Returns a pointer to a dynamically allocated copy of the provided UTF-8 string with all required characters escaped using another specific character. +/// 'chars_to_escape' must represent a NULL-terminated character string with all ASCII characters that need to be escaped. +/// Furthermore, 'escape_char' must represent an ASCII character within the [0x20,0x7E] range. /// Returns NULL if an error occurs. char *utilsEscapeCharacters(const char *str, const char *chars_to_escape, const char escape_char); diff --git a/source/core/devoptab/fat_dev.c b/source/core/devoptab/fat_dev.c index 17b4e73..15ee961 100644 --- a/source/core/devoptab/fat_dev.c +++ b/source/core/devoptab/fat_dev.c @@ -225,7 +225,7 @@ static int fatdev_stat(struct _reent *r, const char *file, struct stat *st) FAT_DEV_INIT_FS_ACCESS; /* Sanity check. */ - if (!file || !st) DEVOPTAB_SET_ERROR_AND_EXIT(EINVAL); + if (!st) DEVOPTAB_SET_ERROR_AND_EXIT(EINVAL); /* Get fixed path. */ if (!(file = fatdev_get_fixed_path(r, file, fs_ctx))) DEVOPTAB_EXIT; @@ -421,7 +421,7 @@ static const char *fatdev_get_fixed_path(struct _reent *r, const char *path, FAT /* Generate fixed path. */ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wformat-truncation" - snprintf(g_fatDevicePathBuffer, sizeof(g_fatDevicePathBuffer), "%s%s", name, path); + snprintf(g_fatDevicePathBuffer, MAX_ELEMENTS(g_fatDevicePathBuffer), "%s%s", name, path); #pragma GCC diagnostic pop //LOG_MSG_DEBUG("Fixed path: \"%s\".", g_fatDevicePathBuffer); diff --git a/source/core/gamecard.c b/source/core/gamecard.c index 2948f5d..cfb33fc 100644 --- a/source/core/gamecard.c +++ b/source/core/gamecard.c @@ -377,8 +377,9 @@ bool gamecardGetCertificate(FsGameCardCertificate *out) if (!g_gameCardInterfaceInit || atomic_load(&g_gameCardStatus) != GameCardStatus_InsertedAndInfoLoaded || !g_gameCardHandle.value || !out) break; /* Read the gamecard certificate using the official IPC call. */ - Result rc = fsDeviceOperatorGetGameCardDeviceCertificate(&g_deviceOperator, &g_gameCardHandle, out, sizeof(FsGameCardCertificate), (s64)sizeof(FsGameCardCertificate)); - if (R_FAILED(rc)) LOG_MSG_ERROR("fsDeviceOperatorGetGameCardDeviceCertificate failed! (0x%X)", rc); + size_t out_size = 0; + Result rc = fsDeviceOperatorGetGameCardDeviceCertificate(&g_deviceOperator, &g_gameCardHandle, out, sizeof(FsGameCardCertificate), (s64*)&out_size, (s64)sizeof(FsGameCardCertificate)); + if (R_FAILED(rc) || out_size != sizeof(FsGameCardCertificate)) LOG_MSG_ERROR("fsDeviceOperatorGetGameCardDeviceCertificate failed! (0x%X, 0x%lX).", rc, out_size); ret = R_SUCCEEDED(rc); } diff --git a/source/core/nxdt_utils.c b/source/core/nxdt_utils.c index c067790..fed6c49 100644 --- a/source/core/nxdt_utils.c +++ b/source/core/nxdt_utils.c @@ -593,7 +593,8 @@ void utilsReplaceIllegalCharacters(char *str, bool ascii_only) units = decode_utf8(&code, ptr1); if (units < 0) break; - if (memchr(g_illegalFileSystemChars, (int)code, g_illegalFileSystemCharsLength) || code < 0x20 || (!ascii_only && code == 0x7F) || (ascii_only && code >= 0x7F)) + if (code < 0x20 || (!ascii_only && code == 0x7F) || (ascii_only && code >= 0x7F) || \ + (units == 1 && memchr(g_illegalFileSystemChars, (int)code, g_illegalFileSystemCharsLength))) { if (!repl) { @@ -618,7 +619,7 @@ char *utilsEscapeCharacters(const char *str, const char *chars_to_escape, const size_t str_size = 0, chars_to_escape_size = 0; if (!str || !(str_size = strlen(str)) || !chars_to_escape || !(chars_to_escape_size = strlen(chars_to_escape)) || \ - escape_char < 0x20 || escape_char >= 0x7F || memchr(chars_to_escape, (int)escape_char, chars_to_escape_size)) + escape_char < 0x20 || escape_char >= 0x7F) { LOG_MSG_ERROR("Invalid parameters!"); return NULL; @@ -630,13 +631,13 @@ char *utilsEscapeCharacters(const char *str, const char *chars_to_escape, const size_t cur_pos = 0, escaped_str_size = 0; char *ret = NULL; - /* Determine the number of character we need to escape. */ + /* Determine the number of characters we need to escape. */ while(cur_pos < str_size) { units = decode_utf8(&code, ptr); if (units < 0) break; - if (memchr(chars_to_escape, (int)code, chars_to_escape_size)) escape_cnt++; + if (units == 1 && memchr(chars_to_escape, (int)code, chars_to_escape_size)) escape_cnt++; ptr += units; cur_pos += (size_t)units; @@ -671,7 +672,7 @@ char *utilsEscapeCharacters(const char *str, const char *chars_to_escape, const units = decode_utf8(&code, ptr); if (units < 0) break; - if (memchr(chars_to_escape, (int)code, chars_to_escape_size)) ret[cur_pos++] = escape_char; + if (units == 1 && memchr(chars_to_escape, (int)code, chars_to_escape_size)) ret[cur_pos++] = escape_char; for(ssize_t i = 0; i < units; i++) ret[cur_pos + (size_t)i] = ptr[i];