From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 25591 invoked by alias); 22 Oct 2016 19:10:42 -0000 Mailing-List: contact newlib-cvs-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: newlib-cvs-owner@sourceware.org Received: (qmail 25517 invoked by uid 9078); 22 Oct 2016 19:10:41 -0000 Date: Sat, 22 Oct 2016 19:10:00 -0000 Message-ID: <20161022191041.25514.qmail@sourceware.org> Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Corinna Vinschen To: newlib-cvs@sourceware.org Subject: [newlib-cygwin] Fix check for empty locale string in newlocale X-Act-Checkin: newlib-cygwin X-Git-Author: Corinna Vinschen X-Git-Refname: refs/heads/master X-Git-Oldrev: dda82d1a7be51719013dd2c9d255a2e7790eed53 X-Git-Newrev: 34aded1f54da8e3035275bce3196cb607d3e386c X-SW-Source: 2016-q4/txt/msg00002.txt.bz2 https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=34aded1f54da8e3035275bce3196cb607d3e386c commit 34aded1f54da8e3035275bce3196cb607d3e386c Author: Corinna Vinschen Date: Sat Oct 22 20:22:20 2016 +0200 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 Diff: --- 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 bd4e385..c817625 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;