From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2138) id 73B42385840D; Mon, 11 Mar 2024 09:01:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 73B42385840D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1710147710; bh=DqUiGQROXtWJwEMoTM29SUMUzU1ytoeOzhT+xNa0pMw=; h=From:To:Subject:Date:From; b=Xs8uIpuGIKzgcyvbF4+BQGum5YI/c8HIVGJ8Wq9EC68hXTWhJGpQK6np4bStjO9nK aCPAXDWXychA4nsGsjIQC/BPCAyUhMp2vtrDpZ7NibJvl9pGJBWvVBYA/Bkv+AqXNd mI9e/8/iLI3QFtrr8R5wg6uVq/8Kmpa07qaThJ3Q= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Andreas Schwab To: glibc-cvs@sourceware.org Subject: [glibc] duplocale: protect use of global locale (bug 23970) X-Act-Checkin: glibc X-Git-Author: Andreas Schwab X-Git-Refname: refs/heads/master X-Git-Oldrev: b6e3898194bbae78910bbe9cd086937014961e45 X-Git-Newrev: 513331b788a3fa633f1d0417d43915e16a0c88f0 Message-Id: <20240311090150.73B42385840D@sourceware.org> Date: Mon, 11 Mar 2024 09:01:50 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=513331b788a3fa633f1d0417d43915e16a0c88f0 commit 513331b788a3fa633f1d0417d43915e16a0c88f0 Author: Andreas Schwab Date: Wed Mar 6 12:59:47 2024 +0100 duplocale: protect use of global locale (bug 23970) 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. Diff: --- 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)