mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Assaf Gordon <assafgordon@gmail.com>
To: musl@lists.openwall.com
Subject: Possible bug in setlocale upon invalid LC_ALL value
Date: Fri, 1 Apr 2016 20:47:01 -0400	[thread overview]
Message-ID: <4C4AEBC7-4344-4867-B8F6-F1A691F123E0@gmail.com> (raw)

Hello musl developers,

I'm testing compilation of GNU coreutils on Alpine Linux 3.3.3 (linux kernel 4.1.20, musl-1.1.12-r3).

I think I've encountered a problem in musl, where using setlocale with invalid locale name returns the invalid locale instead of a known locale.
example:

   $ LC_ALL=missing ./myprogram

If myprogram calls setlocale(LC_ALL,""),
then musl sets the internal locale despite being invalid value.
later, checking the locale for a specific category (e.g. LC_COLLATE) will return 'missing' instead of 'C' .


The relevant POSIX clause is this:
 http://pubs.opengroup.org/onlinepubs/9699919799/functions/setlocale.html
 "[...] If the value of any of these environment variable searches yields a locale that
  is not supported (and non-null), setlocale() shall return a null pointer and the global
  locale shall not be changed."

Below is a short C program demonstrating the issue, with example output from various OSes.

comments welcomed,
 - assaf


/*
Test 'setlocale()' behaviour.

compile:
   cc -o print-locale print-locale.c
test:
   ./print-locale
   LC_ALL=C ./print-locale
   LC_ALL=missing ./print-locale
*/
#include <locale.h>
#include <stdlib.h>
#include <stdio.h>

int main(void)
{
 char* p = getenv("LC_ALL");
 printf("LC_ALL env var = '%s'\n", p?p:"(NULL)");

 p = setlocale(LC_ALL,"");
 printf("setlocale(LC_ALL,\"\") = '%s'\n", p?p:"(NULL)");

 p = setlocale(LC_ALL,NULL);
 printf("LC_ALL from setlocale = '%s'\n", p?p:"(NULL)");

 p = setlocale(LC_COLLATE,NULL);
 printf("LC_COLLATE from setlocale = '%s'\n", p?p:"(NULL)");

 return 0;
}

==== musl libc =======

$ ./print-locale
LC_ALL env var = '(NULL)'
setlocale(LC_ALL,"") = 'C.UTF-8;C;C;C;C;C'
LC_ALL from setlocale = 'C.UTF-8;C;C;C;C;C'
LC_COLLATE from setlocale = 'C'

$ LC_ALL=c ./print-locale
LC_ALL env var = 'C'
setlocale(LC_ALL,"") = 'C;C;C;C;C;C'
LC_ALL from setlocale = 'C;C;C;C;C;C'
LC_COLLATE from setlocale = 'C'

$ LC_ALL=missing ./print-locale
LC_ALL env var = 'missing'
setlocale(LC_ALL,"") = 'missing;missing;missing;missing;missing;missing'
LC_ALL from setlocale = 'missing;missing;missing;missing;missing;missing'
LC_COLLATE from setlocale = 'missing'


==== glibc (Ubuntu) ====

$ ./print-locale
LC_ALL env var = '(NULL)'
setlocale(LC_ALL,"") = 'en_US.UTF-8'
LC_ALL from setlocale = 'en_US.UTF-8'
LC_COLLATE from setlocale = 'en_US.UTF-8'

$ LC_ALL=C ./print-locale
LC_ALL env var = 'C'
setlocale(LC_ALL,"") = 'C'
LC_ALL from setlocale = 'C'
LC_COLLATE from setlocale = 'C'

$ LC_ALL=missing ./print-locale
LC_ALL env var = 'missing'
setlocale(LC_ALL,"") = '(NULL)'
LC_ALL from setlocale = 'C'
LC_COLLATE from setlocale = 'C'

==== FreeBSD 10.1 ====

$ ./print-locale
LC_ALL env var = '(NULL)'
setlocale(LC_ALL,"") = 'C'
LC_ALL from setlocale = 'C'
LC_COLLATE from setlocale = 'C'

$ LC_ALL=C ./print-locale
LC_ALL env var = 'C'
setlocale(LC_ALL,"") = 'C'
LC_ALL from setlocale = 'C'
LC_COLLATE from setlocale = 'C'

$ LC_ALL=missing ./print-locale
LC_ALL env var = 'missing'
setlocale(LC_ALL,"") = '(NULL)'
LC_ALL from setlocale = 'C'
LC_COLLATE from setlocale = 'C'


==== OpenBSD 5.8 ====

$ ./print-locale
LC_ALL env var = '(NULL)'
setlocale(LC_ALL,"") = 'C'
LC_ALL from setlocale = 'C'
LC_COLLATE from setlocale = 'C'

$ LC_ALL=C ./print-locale
LC_ALL env var = 'C'
setlocale(LC_ALL,"") = 'C'
LC_ALL from setlocale = 'C'
LC_COLLATE from setlocale = 'C'

$ LC_ALL=missing ./print-locale
LC_ALL env var = 'missing'
setlocale(LC_ALL,"") = 'C/missing/C/C/C/C'
LC_ALL from setlocale = 'C/missing/C/C/C/C'
LC_COLLATE from setlocale = 'C'

==== AIX 7 ===

$ ./print-locale
LC_ALL env var = '(NULL)'
setlocale(LC_ALL,"") = 'en_US en_US en_US en_US en_US en_US'
LC_ALL from setlocale = 'en_US en_US en_US en_US en_US en_US'
LC_COLLATE from setlocale = 'en_US'

$ LC_ALL=C ./print-locale
LC_ALL env var = 'C'
setlocale(LC_ALL,"") = 'C C C C C C'
LC_ALL from setlocale = 'C C C C C C'
LC_COLLATE from setlocale = 'C'

$ LC_ALL=missing ./print-locale
LC_ALL env var = 'missing'
setlocale(LC_ALL,"") = '(NULL)'
LC_ALL from setlocale = 'C C C C C C'
LC_COLLATE from setlocale = 'C'


             reply	other threads:[~2016-04-02  0:47 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-02  0:47 Assaf Gordon [this message]
2016-04-02  0:58 ` Rich Felker
2016-04-02  2:46   ` Assaf Gordon
2016-04-02  4:09     ` Rich Felker
2016-04-02  4:18       ` Assaf Gordon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4C4AEBC7-4344-4867-B8F6-F1A691F123E0@gmail.com \
    --to=assafgordon@gmail.com \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).