From 34aded1f54da8e3035275bce3196cb607d3e386c Mon Sep 17 00:00:00 2001 From: Corinna Vinschen Date: Sat, 22 Oct 2016 20:22:20 +0200 Subject: [PATCH] Fix check for empty locale string in newlocale The original test is broken. It tests for a NULL locale which isn't just wrong, it simply can't occur at this point due to an earlier check for a NULL locale string. Thus, the locale info for a category is never taken from the environment. Fixes Coverty CID 153467. Also, add comment. Also, add some parens for readability. Signed-off-by: Corinna Vinschen --- newlib/libc/locale/newlocale.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/newlib/libc/locale/newlocale.c b/newlib/libc/locale/newlocale.c index bd4e38584..c8176256e 100644 --- a/newlib/libc/locale/newlocale.c +++ b/newlib/libc/locale/newlocale.c @@ -101,7 +101,7 @@ _newlocale_r (struct _reent *p, int category_mask, const char *locale, category_mask |= LC_VALID_MASK; } /* Check for invalid mask values and valid locale ptr. */ - if (category_mask & ~LC_VALID_MASK || !locale) + if ((category_mask & ~LC_VALID_MASK) || !locale) { p->_errno = EINVAL; return NULL; @@ -119,7 +119,10 @@ _newlocale_r (struct _reent *p, int category_mask, const char *locale, { if (((1 << i) & category_mask) != 0) { - const char *cat = locale ?: __get_locale_env (p, i); + /* If locale is "", fetch from environment. Otherwise use locale + name verbatim. */ + const char *cat = (locale[0] == '\0') ? __get_locale_env (p, i) + : locale; if (strlen (cat) > ENCODING_LEN) { p->_errno = EINVAL;