Patch: fix for setlocale problem

Mike FABIAN mfabian at suse.de
Mon Mar 31 06:27:30 EST 2003


The attached patch fixes a problem with the usage of setlocale.

The original code has some problems:

1) it changes the string returned by setlocale() directly without
making a copy, which is not allowed according to the glibc info pages:

glibc info>     The string returned by `setlocale' can be overwritten by subsequent
glibc info>     calls, so you should make a copy of the string (*note Copying and
glibc info>     Concatenation::) if you want to save it past any further calls to
glibc info>     `setlocale'.  (The standard library is guaranteed never to call
glibc info>     `setlocale' itself.)
glibc info>
glibc info>     You should not modify the string returned by `setlocale'.  It might
glibc info>     be the same string that was passed as an argument in a previous
glibc info>     call to `setlocale'.  One requirement is that the CATEGORY must be
glibc info>     the same in the call the string was returned and the one when the
glibc info>     string is passed in as LOCALE parameter.

2> it parses the return value of setlocale(LC_ALL, "") which isn't
allowed either:

glibc info>     When you read the current locale for category `LC_ALL', the value
glibc info>     encodes the entire combination of selected locales for all
glibc info>     categories.  In this case, the value is not just a single locale
glibc info>     name.  In fact, we don't make any promises about what it looks like.
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
glibc info>     But if you specify the same "locale name" with `LC_ALL' in
glibc info>     a subsequent call to `setlocale', it restores the same combination
glibc info>     of locale selections.

Indeed, this causes a problem on some platforms with the current
openbox. Let's have a look at the return values of
setlocale(LC_ALL,"") on different systems (the test program
"setlocale.c" which is used in the following example is attached).

First on a AMD x86_64:

    mfabian at magellan:~/tmp$ uname -a
    Linux magellan 2.4.20 #1 Sun Mar 23 07:16:53 UTC 2003 x86_64 unknown unknown GNU/Linux
    mfabian at magellan:~/tmp$ gcc setlocale.c
    mfabian at magellan:~/tmp$ ./a.out
    return value of setlocale()=ja_JP.UTF-8

Now the same program on an i686 platform:

    mfabian at gregory:~$ uname -a
    Linux gregory 2.4.20-4GB #1 Mon Mar 17 17:54:44 UTC 2003 i686 unknown unknown GNU/Linux
    mfabian at gregory:~/tmp$ gcc setlocale.c
    gcc setlocale.c
    mfabian at gregory:~/tmp$ ./a.out
    ./a.out
    return value of setlocale()=LC_CTYPE=ja_JP.UTF-8;LC_NUMERIC=ja_JP.UTF-8;LC_TIME=ja_JP.UTF-8;LC_COLLATE=C;LC_MONETARY=ja_JP.UTF-8;LC_MESSAGES=ja_JP.UTF-8;LC_PAPER=ja_JP.UTF-8;LC_NAME=ja_JP.UTF-8;LC_ADDRESS=ja_JP.UTF-8;LC_TELEPHONE=ja_JP.UTF-8;LC_MEASUREMENT=ja_JP.UTF-8;LC_IDENTIFICATION=ja_JP.UTF-8
    mfabian at gregory:~/c$ 

The return value of setlocale(LC_ALL, "") is different ("no promises
about what it looks like"!).

Currently openbox uses the return value of setlocale(LC_ALL, "")
cut before the first '.' character to find out which translation
file to read. This happens to work on the AMD x86_64 but fails
on i686. On i686 openbox tries to open 

    /usr/share/openbox/nls/LC_CTYPE=ja_JP/openbox.cat

which doesn't exist of course, therefore the fallback to English is
used (verified with "strace -eopen openbox").

I believe my patch openbox-2.2.3-setlocale.diff solves the above
problems.

First it calls setlocale(LC_ALL, "") to make use of all locales
specified by the environment variables, but doesn't use the return
value.

Then it just queries the value of LC_MESSAGES by calling
setlocale(LC_MESSAGES, NULL) and makes a copy of the return value
which is then modified to find the translation file to use. The return
value of setlocale(LC_MESSAGES, "") doesn't have the problem
demonstrated above for the return value of setlocale(LC_ALL,""), it
looks like it can safely be parsed to find the language subdirectory
where to find the openbox.cat file with the translated messages.

According to POSIX, LC_MESSAGES should be used to determine
the language of the messages, see also

http://www.opengroup.org/onlinepubs/007904975/basedefs/xbd_chap08.html


-------------- next part --------------
A non-text attachment was scrubbed...
Name: openbox-2.2.3-setlocale.diff
Type: text/x-patch
Size: 856 bytes
Desc: not available
URL: <http://icculus.org/pipermail/openbox/attachments/20030331/7cf559d2/attachment.bin>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: setlocale.c
URL: <http://icculus.org/pipermail/openbox/attachments/20030331/7cf559d2/attachment.diff>
-------------- next part --------------

-- 
Mike Fabian   <mfabian at suse.de>   http://www.suse.de/~mfabian
?????????????


More information about the openbox mailing list