Cleaned up code in locale for safety

This commit is contained in:
NeGate 2022-06-02 16:29:14 -04:00
parent 55d39d289d
commit 843f13b504
1 changed files with 53 additions and 45 deletions

View File

@ -11,17 +11,22 @@
#define _LC_LAST 5 #define _LC_LAST 5
static struct lconv _locale; static struct lconv _locale;
static const char *_locale_str; static char _locale_str[16];
char *setlocale(int category, const char *locale) // Even if the user doesn't enable LIB_EXT1 we still have it existing
{ size_t strnlen_s(const char *s, size_t maxsize);
assert(_LC_FIRST <= category && category <= _LC_LAST);
char *setlocale(int category, const char *locale) {
if (_LC_FIRST <= category && category <= _LC_LAST) return NULL;
if (locale == NULL) return _locale_str;
// Validate the string a bit
size_t locale_len = strnlen_s(locale, sizeof(_locale_str));
if (locale_len == 0 || locale_len >= sizeof(_locale_str)) return NULL;
if(locale == NULL) {
return _locale_str;
}
if(strcmp(locale, "C") == 0) { if(strcmp(locale, "C") == 0) {
if(category == LC_ALL) { switch (category) {
case LC_ALL: {
_locale.decimal_point = "."; _locale.decimal_point = ".";
_locale.thousands_sep = ""; _locale.thousands_sep = "";
_locale.grouping = ""; _locale.grouping = "";
@ -46,16 +51,19 @@ char *setlocale(int category, const char *locale)
_locale.int_n_sep_by_space = CHAR_MAX; _locale.int_n_sep_by_space = CHAR_MAX;
_locale.int_p_sign_posn = CHAR_MAX; _locale.int_p_sign_posn = CHAR_MAX;
_locale.int_n_sign_posn = CHAR_MAX; _locale.int_n_sign_posn = CHAR_MAX;
break;
} }
default: return NULL;
} }
else { } else {
return NULL; return NULL;
} }
_locale_str = locale;
return locale; memcpy(_locale_str, locale, locale_len);
return _locale_str;
} }
struct lconv *localeconv(void) struct lconv *localeconv(void) {
{ return &_locale;
} }