From c594e8801d116e49e025fbfde04cce84791bf8b9 Mon Sep 17 00:00:00 2001 From: Bernhard Schelling <14200249+schellingb@users.noreply.github.com> Date: Thu, 19 Nov 2020 23:42:05 +0900 Subject: [PATCH] Fix ctype style char macros with signed inputs UTF8 byte codes are commonly stored in signed char types thus these macros could be called with negative numbers. Avoid invalid memory access by clamping the input values to 0 ~ 255 with an unsigned char cast. --- libretro-common/include/string/stdstring.h | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/libretro-common/include/string/stdstring.h b/libretro-common/include/string/stdstring.h index 5d8c31b322..82e038bdee 100644 --- a/libretro-common/include/string/stdstring.h +++ b/libretro-common/include/string/stdstring.h @@ -44,20 +44,20 @@ RETRO_BEGIN_DECLS #define string_is_not_equal_fast(a, b, size) (memcmp(a, b, size) != 0) #define string_is_equal_fast(a, b, size) (memcmp(a, b, size) == 0) -#define TOLOWER(c) (c | (lr_char_props[c] & 0x20)) -#define TOUPPER(c) (c & ~(lr_char_props[c] & 0x20)) +#define TOLOWER(c) ((c) | (lr_char_props[(unsigned char)(c)] & 0x20)) +#define TOUPPER(c) ((c) & ~(lr_char_props[(unsigned char)(c)] & 0x20)) /* C standard says \f \v are space, but this one disagrees */ -#define ISSPACE(c) (lr_char_props[c] & 0x80) +#define ISSPACE(c) (lr_char_props[(unsigned char)(c)] & 0x80) -#define ISDIGIT(c) (lr_char_props[c] & 0x40) -#define ISALPHA(c) (lr_char_props[c] & 0x20) -#define ISLOWER(c) (lr_char_props[c] & 0x04) -#define ISUPPER(c) (lr_char_props[c] & 0x02) -#define ISALNUM(c) (lr_char_props[c] & 0x60) -#define ISUALPHA(c) (lr_char_props[c] & 0x28) -#define ISUALNUM(c) (lr_char_props[c] & 0x68) -#define IS_XDIGIT(c) (lr_char_props[c] & 0x01) +#define ISDIGIT(c) (lr_char_props[(unsigned char)(c)] & 0x40) +#define ISALPHA(c) (lr_char_props[(unsigned char)(c)] & 0x20) +#define ISLOWER(c) (lr_char_props[(unsigned char)(c)] & 0x04) +#define ISUPPER(c) (lr_char_props[(unsigned char)(c)] & 0x02) +#define ISALNUM(c) (lr_char_props[(unsigned char)(c)] & 0x60) +#define ISUALPHA(c) (lr_char_props[(unsigned char)(c)] & 0x28) +#define ISUALNUM(c) (lr_char_props[(unsigned char)(c)] & 0x68) +#define IS_XDIGIT(c) (lr_char_props[(unsigned char)(c)] & 0x01) /* Deprecated alias, all callers should use string_is_equal_case_insensitive instead */ #define string_is_equal_noncase string_is_equal_case_insensitive