public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] duplocale: protect use of global locale (bug 23970)
@ 2024-03-06 12:48 Andreas Schwab
  2024-03-07 20:37 ` Carlos O'Donell
  0 siblings, 1 reply; 2+ messages in thread
From: Andreas Schwab @ 2024-03-06 12:48 UTC (permalink / raw)
  To: libc-alpha

Protect the global locale from being modified while we compute the size of
the locale category names.  That allows the use of the global locale in a
single thread, while all other threads use the thread safe locale
functions.
---
 locale/duplocale.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/locale/duplocale.c b/locale/duplocale.c
index a755ac5c36..bad476700f 100644
--- a/locale/duplocale.c
+++ b/locale/duplocale.c
@@ -43,6 +43,11 @@ __duplocale (locale_t dataset)
   int cnt;
   size_t names_len = 0;
 
+  /* If dataset points to _nl_global_locale, we need to prevent other
+     threads from modifying it.  We also modify global data below (the
+     usage counts).  */
+  __libc_rwlock_wrlock (__libc_setlocale_lock);
+
   /* Calculate the total space we need to store all the names.  */
   for (cnt = 0; cnt < __LC_LAST; ++cnt)
     if (cnt != LC_ALL && dataset->__names[cnt] != _nl_C_name)
@@ -55,9 +60,6 @@ __duplocale (locale_t dataset)
     {
       char *namep = (char *) (result + 1);
 
-      /* We modify global data (the usage counts).  */
-      __libc_rwlock_wrlock (__libc_setlocale_lock);
-
       for (cnt = 0; cnt < __LC_LAST; ++cnt)
 	if (cnt != LC_ALL)
 	  {
@@ -78,11 +80,11 @@ __duplocale (locale_t dataset)
       result->__ctype_b = dataset->__ctype_b;
       result->__ctype_tolower = dataset->__ctype_tolower;
       result->__ctype_toupper = dataset->__ctype_toupper;
-
-      /* It's done.  */
-      __libc_rwlock_unlock (__libc_setlocale_lock);
     }
 
+  /* It's done.  */
+  __libc_rwlock_unlock (__libc_setlocale_lock);
+
   return result;
 }
 weak_alias (__duplocale, duplocale)
-- 
2.44.0


-- 
Andreas Schwab, SUSE Labs, schwab@suse.de
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] duplocale: protect use of global locale (bug 23970)
  2024-03-06 12:48 [PATCH] duplocale: protect use of global locale (bug 23970) Andreas Schwab
@ 2024-03-07 20:37 ` Carlos O'Donell
  0 siblings, 0 replies; 2+ messages in thread
From: Carlos O'Donell @ 2024-03-07 20:37 UTC (permalink / raw)
  To: Andreas Schwab, libc-alpha

On 3/6/24 07:48, Andreas Schwab wrote:
> Protect the global locale from being modified while we compute the size of
> the locale category names.  That allows the use of the global locale in a
> single thread, while all other threads use the thread safe locale
> functions.

Agreed. Good catch, we should make this work.

LGTM.

Reviewed-by: Carlos O'Donell <carlos@redhat.com>

> ---
>  locale/duplocale.c | 14 ++++++++------
>  1 file changed, 8 insertions(+), 6 deletions(-)
> 
> diff --git a/locale/duplocale.c b/locale/duplocale.c
> index a755ac5c36..bad476700f 100644
> --- a/locale/duplocale.c
> +++ b/locale/duplocale.c
> @@ -43,6 +43,11 @@ __duplocale (locale_t dataset)
>    int cnt;
>    size_t names_len = 0;
>  
> +  /* If dataset points to _nl_global_locale, we need to prevent other
> +     threads from modifying it.  We also modify global data below (the
> +     usage counts).  */
> +  __libc_rwlock_wrlock (__libc_setlocale_lock);
> +
>    /* Calculate the total space we need to store all the names.  */
>    for (cnt = 0; cnt < __LC_LAST; ++cnt)
>      if (cnt != LC_ALL && dataset->__names[cnt] != _nl_C_name)
> @@ -55,9 +60,6 @@ __duplocale (locale_t dataset)
>      {
>        char *namep = (char *) (result + 1);
>  
> -      /* We modify global data (the usage counts).  */
> -      __libc_rwlock_wrlock (__libc_setlocale_lock);
> -
>        for (cnt = 0; cnt < __LC_LAST; ++cnt)
>  	if (cnt != LC_ALL)
>  	  {
> @@ -78,11 +80,11 @@ __duplocale (locale_t dataset)
>        result->__ctype_b = dataset->__ctype_b;
>        result->__ctype_tolower = dataset->__ctype_tolower;
>        result->__ctype_toupper = dataset->__ctype_toupper;
> -
> -      /* It's done.  */
> -      __libc_rwlock_unlock (__libc_setlocale_lock);
>      }
>  
> +  /* It's done.  */
> +  __libc_rwlock_unlock (__libc_setlocale_lock);
> +
>    return result;
>  }
>  weak_alias (__duplocale, duplocale)

-- 
Cheers,
Carlos.


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-03-07 20:37 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-06 12:48 [PATCH] duplocale: protect use of global locale (bug 23970) Andreas Schwab
2024-03-07 20:37 ` Carlos O'Donell

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 read-only IMAP folder(s) and NNTP newsgroup(s).