Skip to content

Commit

Permalink
Add fallback in case of locale initialization failure
Browse files Browse the repository at this point in the history
Reviewed-by: Dmitry Belyavskiy <beldmit@gmail.com>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from #18282)
  • Loading branch information
t8m committed May 13, 2022
1 parent 26ccb0e commit 92d0501
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
21 changes: 16 additions & 5 deletions crypto/o_str.c
Expand Up @@ -349,8 +349,8 @@ int openssl_strerror_r(int errnum, char *buf, size_t buflen)
#ifndef OPENSSL_NO_LOCALE
static locale_t loc;

static void *ossl_c_locale(void) {
return (void *)loc;
static locale_t ossl_c_locale(void) {
return loc;
}

int ossl_init_casecmp_int(void) {
Expand All @@ -359,21 +359,32 @@ int ossl_init_casecmp_int(void) {
# else
loc = newlocale(LC_COLLATE_MASK, "C", (locale_t) 0);
# endif
return (loc == (locale_t) 0) ? 0 : 1;
return (loc == (locale_t)0) ? 0 : 1;
}

void ossl_deinit_casecmp(void) {
freelocale(loc);
loc = (locale_t)0;
}

int OPENSSL_strcasecmp(const char *s1, const char *s2)
{
return strcasecmp_l(s1, s2, (locale_t)ossl_c_locale());
locale_t l = ossl_c_locale();

/* Fallback in case of locale initialization failure */
if (l == (locale_t)0)
return strcasecmp(s1, s2);
return strcasecmp_l(s1, s2, l);
}

int OPENSSL_strncasecmp(const char *s1, const char *s2, size_t n)
{
return strncasecmp_l(s1, s2, n, (locale_t)ossl_c_locale());
locale_t l = ossl_c_locale();

/* Fallback in case of locale initialization failure */
if (l == (locale_t)0)
return strncasecmp(s1, s2, n);
return strncasecmp_l(s1, s2, n, l);
}
#else
int ossl_init_casecmp_int(void) {
Expand Down
1 change: 1 addition & 0 deletions include/internal/e_os.h
Expand Up @@ -421,6 +421,7 @@ inline int nssgetpid();
# define strcasecmp_l _stricmp_l
# define strncasecmp_l _strnicmp_l
# define strcasecmp _stricmp
# define strncasecmp _strnicmp
# elif !defined(_POSIX_C_SOURCE) || _POSIX_C_SOURCE < 200809L \
|| defined(OPENSSL_SYS_TANDEM)
# ifndef OPENSSL_NO_LOCALE
Expand Down

0 comments on commit 92d0501

Please sign in to comment.