From: He X <xw897002528@gmail.com>
To: musl@lists.openwall.com
Subject: Re: [setlocale]: return only one copy if all six parts of locale are same
Date: Sat, 18 Mar 2017 21:48:41 +0800 [thread overview]
Message-ID: <CAPG2z08Z2V08wQPT0EN27Fgc97Ac14VNxQXQOjsfC2oK2+wz0Q@mail.gmail.com> (raw)
In-Reply-To: <CAPG2z09AuL34DODuEBVfNy4CXKatj2HivbMMxN_gphZzyt9ecw@mail.gmail.com>
[-- Attachment #1.1: Type: text/plain, Size: 2782 bytes --]
oops, i missed UNLOCK(lock).
2017-03-18 21:36 GMT+08:00 He X <xw897002528@gmail.com>:
> tested:
> [xhe@xhe-PC locale]$ ./a.out
> C
> zh_CN.UTF-8
> zh_CN.UTF-8
> [xhe@xhe-PC locale]$ cat test.c
> #include <locale.h>
> int main() {
> printf("%s\n", setlocale(LC_ALL, NULL));
> printf("%s\n", setlocale(LC_ALL, ""));
> printf("%s\n", setlocale(LC_ALL, NULL));
> }
>
> 2017-03-18 20:33 GMT+08:00 Rich Felker <dalias@libc.org>:
>
>> On Sat, Mar 18, 2017 at 07:37:57AM +0000, He X wrote:
>> > As i suggest on IRC, here's the patch.
>>
>> > --- musl-1.1.16/src/locale/setlocale.c 2017-03-17
>> 17:49:15.767952411 +0000
>> > +++ musl-1.1.16/src/locale/setlocale.c 2017-03-17
>> 17:49:15.767952411 +0000
>> > @@ -48,16 +48,33 @@
>> > }
>> > }
>> > char *s = buf;
>> > - for (i=0; i<LC_ALL; i++) {
>> > + const struct __locale_map *flm =
>> > + libc.global_locale.cat[0];
>> > + const char *fpart = flm ? flm->name : "C";
>> > + size_t l = strlen(fpart);
>> > + memcpy(s, fpart, l);
>> > + s[l] = 0;
>> > + i=1;
>> > + do {
>> > const struct __locale_map *lm =
>> > libc.global_locale.cat[i];
>> > const char *part = lm ? lm->name : "C";
>> > - size_t l = strlen(part);
>> > - memcpy(s, part, l);
>> > - s[l] = ';';
>> > - s += l+1;
>> > + if (strcmp(s, part)) break;
>> > + i++;
>> > + } while (i<LC_ALL);
>> > +
>> > + if (i != LC_ALL) {
>> > + for (i=0; i<LC_ALL; i++) {
>> > + const struct __locale_map *lm =
>> > + libc.global_locale.cat[i];
>> > + const char *part = lm ? lm->name : "C";
>> > + size_t l = strlen(part);
>> > + memcpy(s, part, l);
>> > + s[l] = ';';
>> > + s += l+1;
>> > + }
>> > + *--s = 0;
>> > }
>> > - *--s = 0;
>> > UNLOCK(lock);
>> > return buf;
>> > }
>>
>> I think the results of this patch can be achieved much more simply.
>> Just compare lm pointers (rather than string contents) for equality at
>> each iteration in the current loop, counting how many were equal to
>> libc.global_locale.cat[0]. If that number equals LC_ALL, return
>> libc.global_locale.cat[0]->name (or "C" if the lm is null) rather than
>> buf.
>>
>> Rich
>>
>
>
[-- Attachment #1.2: Type: text/html, Size: 5041 bytes --]
[-- Attachment #2: setlocale.patch --]
[-- Type: text/x-patch, Size: 980 bytes --]
--- musl-1.1.16/src/locale/setlocale.c 2017-03-17 17:49:15.767952411 +0000
+++ musl-1.1.16/src/locale/setlocale.c 2017-03-17 17:49:15.767952411 +0000
@@ -48,16 +48,31 @@
}
}
char *s = buf;
- for (i=0; i<LC_ALL; i++) {
+ const struct __locale_map *flm =
+ libc.global_locale.cat[0];
+ i=1;
+ do {
const struct __locale_map *lm =
libc.global_locale.cat[i];
- const char *part = lm ? lm->name : "C";
- size_t l = strlen(part);
- memcpy(s, part, l);
- s[l] = ';';
- s += l+1;
+ if (lm != flm) break;
+ i++;
+ } while (i<LC_ALL);
+
+ if (i == LC_ALL) {
+ UNLOCK(lock);
+ return flm ? flm->name : "C";
+ } else {
+ for (i=0; i<LC_ALL; i++) {
+ const struct __locale_map *lm =
+ libc.global_locale.cat[i];
+ const char *part = lm ? lm->name : "C";
+ size_t l = strlen(part);
+ memcpy(s, part, l);
+ s[l] = ';';
+ s += l+1;
+ }
+ *--s = 0;
}
- *--s = 0;
UNLOCK(lock);
return buf;
}
next prev parent reply other threads:[~2017-03-18 13:48 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-03-18 7:37 He X
2017-03-18 12:33 ` Rich Felker
2017-03-18 13:36 ` He X
2017-03-18 13:48 ` He X [this message]
2017-03-18 14:39 ` Rich Felker
2017-03-18 15:00 ` He X
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=CAPG2z08Z2V08wQPT0EN27Fgc97Ac14VNxQXQOjsfC2oK2+wz0Q@mail.gmail.com \
--to=xw897002528@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).