Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uYwd3-00Dq0t-MF for pgsql-hackers@arkaria.postgresql.org; Tue, 08 Jul 2025 00:56:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1uYwd1-005Odq-Nr for pgsql-hackers@arkaria.postgresql.org; Tue, 08 Jul 2025 00:56:12 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uYwd1-005Ode-6v for pgsql-hackers@lists.postgresql.org; Tue, 08 Jul 2025 00:56:12 +0000 Received: from mail-oi1-x233.google.com ([2607:f8b0:4864:20::233]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uYwcw-0068GO-0U for pgsql-hackers@postgresql.org; Tue, 08 Jul 2025 00:56:10 +0000 Received: by mail-oi1-x233.google.com with SMTP id 5614622812f47-408d14c7e92so2928854b6e.3 for ; Mon, 07 Jul 2025 17:56:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=j-davis-com.20230601.gappssmtp.com; s=20230601; t=1751936165; x=1752540965; darn=postgresql.org; h=mime-version:user-agent:references:in-reply-to:date:cc:to:from :subject:message-id:from:to:cc:subject:date:message-id:reply-to; bh=Lwr1QyhHS6cRnBmCq5chAu/CeDCri5n1GtCj2fxwDFM=; b=CaA+fxKQCXbza9f4L2ng/KVt/6qunAC4WlLJT9vqYMtppGtVAgY9n1ykd2eokXi7oN RFd7vA3jtAwTj3/zvGBDd98IyZ9e9Q5MpxS7l/Pe2ygRkR5pRrfcN7UXEkI6jNPAja7b m2a0WpvnwFwO/+n3aeEKEPaOPAtpXmfhrE/bEmlILINAaMhBqFc8CRJS9YuYQgo4T4Al 4tFxyrpVdXgsw2/wpdItHG7mZ48+xZ1+OAqcb3URqYr1e7N/ogk0a/rsz+IYateOFzT2 TTzE8kSMMieDbv/+fYdi1vCP80zFxwZtvQR0l1mn9AIWJs6pPq58NQA0uKCee660dcmQ m/hw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1751936165; x=1752540965; h=mime-version:user-agent:references:in-reply-to:date:cc:to:from :subject:message-id:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=Lwr1QyhHS6cRnBmCq5chAu/CeDCri5n1GtCj2fxwDFM=; b=weBMeCX0e2BBg/LmSJaNlAec1B9+KVaxtdMSigE8fto8a7YosBKUHD0XuPcZdQwISL DwkJR+0h14gTX7XmrC1xRpszQql2DlBeTkZRMNWfEo3GAFGap8emgJy7i5eSQte9qQS1 XGnX0k0wk6WoOy7It2FvYCKx8DiGgLum8GAI9wgpUnDc9MpZBz2aZX3q2+a8q2Gq2FZL cZ2legiQwWjf06brS80rWfrK4BusHXSZLjyLM04nKtkmxwg+q3qG/Ag5KAfe53hy6nmC 5HlJsDPkeS0AKK9ENlihV/Oxt7LOq35fbqdR4zgce4I34+OhWIJkxUewRRE1m+22QgXd Qo+A== X-Forwarded-Encrypted: i=1; AJvYcCWckWHdCl66JzaGYFobYrCUCHjJlnZ6KI11lzZpLRqxbZf+X4HYB70+U9DLLnkSz7DKJLKu0SAv/NTQAVF2@postgresql.org X-Gm-Message-State: AOJu0YxYeOE8+3NB5IB9NzbSeu+UIHL42Gem1NU8iVAzcIpL60nZk2Su S4tXBKaNjg9VTgKhNxXP8s3Z4psiPJkMNddEtRX7dOsxYEvoAcrOFl2Ojqgjd9gImw== X-Gm-Gg: ASbGncu9Ce5/rAIzAtH2tR7QnQ3Deoh4cKoh34rcAEq+syvvfwrglTH28KilRk+Kp8s o1O2YxlEmkA3UiIM0SEGuVuijDXjcl8x2o1SuVFsTmJahx+K2jRGYojgTyye1IsnVNoDDEHOn/f t/R7nZAvprBzOort8nuzxEWYmG72h9sXQDidV7n6h81i0AT6VYJ05918/rfmMHF9x/pcV+ZT0Bd MrcCffmMDbce2WEPeW03lbPoanGk3CF3zpo+ib0cIjIZNDTYs83tg8U0ITSRgf1HBTw/WmC/CxD W1IdVi7qSJxAr8SJ99Si62u3wygfXgVrDWfNNP0cLhzpwN3/fH/vxhBNnlMwdvs23pV8H36odil pZ1JJoJE9WiJXLKn9yNClkF0eBfLEXxe7 X-Google-Smtp-Source: AGHT+IFGaUXsGNzAVuUwZVL04vBx//wuLLydkTnavF1IISe87HBgLJbnOAOEYfEr+jhwDECabqUi6g== X-Received: by 2002:a05:6808:1b21:b0:40a:a971:3918 with SMTP id 5614622812f47-40d073f722cmr10670728b6e.38.1751936165241; Mon, 07 Jul 2025 17:56:05 -0700 (PDT) Received: from jeff-ws-bridge.lan (c-76-102-242-158.hsd1.ca.comcast.net. [76.102.242.158]) by smtp.gmail.com with ESMTPSA id 5614622812f47-40d02adbd55sm1491784b6e.40.2025.07.07.17.56.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Jul 2025 17:56:04 -0700 (PDT) Message-ID: <6956823dd669abc64183fc91e64dc2e56e31f3ea.camel@j-davis.com> Subject: Re: Remaining dependency on setlocale() From: Jeff Davis To: Peter Eisentraut , Thomas Munro Cc: Tom Lane , pgsql-hackers@postgresql.org Date: Mon, 07 Jul 2025 17:56:03 -0700 In-Reply-To: References: <4c5da86af36a0d5e430eee3f60ce5e06f1b5cd34.camel@j-davis.com> <1841102.1722982989@sss.pgh.pa.us> <9875f7f9-50f1-4b5d-86fc-ee8b03e8c162@eisentraut.org> Content-Type: multipart/mixed; boundary="=-DpMW8zU8xd0rsNr9YhLc" User-Agent: Evolution 3.52.3-0ubuntu1 MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --=-DpMW8zU8xd0rsNr9YhLc Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, 2025-06-11 at 12:15 -0700, Jeff Davis wrote: > > v1-0008-Set-process-LC_COLLATE-C-and-LC_CTYPE-C.patch > >=20 > > As I mentioned earlier in the thread, I don't think we can do this > > for=20 > > LC_CTYPE, because otherwise system error messages would not come > > out > > in=20 > > the right encoding. >=20 > Changed it so that it only sets LC_COLLATE to C, and leaves LC_CTYPE > set to datctype. >=20 > Unfortunately, as long as LC_CTYPE is set to a real locale, there's a > danger of accidentally depending on that setting. Can the encoding be > controlled with LC_MESSAGES instead of LC_CTYPE? >=20 > Do you have an example of how things can go wrong? I looked into this a bit, and if I understand correctly, the only problem is with strerror() and strerror_r(), which depend on LC_MESSAGES for the language but LC_CTYPE to find the right encoding. I attached some example C code to illustrate how strerror() is affected by both LC_MESSAGES and LC_CTYPE. For example: $ ./strerror de_DE.UTF-8 de_DE.UTF-8 LC_CTYPE set to: de_DE.UTF-8 LC_MESSAGES set to: de_DE.UTF-8 Error message (from strerror(EILSEQ)): Ung=C3=BCltiges oder unvollst=C3=A4ndiges Multi-Byte- oder Wide-Zeichen $ ./strerror C de_DE.UTF-8 LC_CTYPE set to: C LC_MESSAGES set to: de_DE.UTF-8 Error message (from strerror(EILSEQ)): Ung?ltiges oder unvollst?ndiges Multi-Byte- oder Wide-Zeichen On unix-based systems, we can use newlocale() to initialize a global variable with both LC_CTYPE and LC_MESSAGES set. The LC_MESSAGES portion would need to be updated every time the GUC changes, which is not great. Windows would be a different story, though: strerror() doesn't seem to have a variant that accepts a _locale_t object, and even if it did, I don't see a way to create a _locale_t object with LC_MESSAGES and LC_CTYPE set to different values. One idea is to use _configthreadlocale(_ENABLE_PER_THREAD_LOCALE), and then use setlocale(), which could enable us to use setlocale() similar to how we use uselocale() on other systems. That would be awkward, though. Thoughts? That seems like a lot of work just for the case of strerror()/strerror_r(). Regards, Jeff Davis [1] https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/configthr= eadlocale?view=3Dmsvc-170 --=-DpMW8zU8xd0rsNr9YhLc Content-Disposition: attachment; filename="strerror.c" Content-Type: text/x-csrc; name="strerror.c"; charset="UTF-8" Content-Transfer-Encoding: base64 I2luY2x1ZGUgPGVycm5vLmg+CiNpbmNsdWRlIDxsb2NhbGUuaD4KI2luY2x1ZGUgPHN0ZGlvLmg+ CiNpbmNsdWRlIDxzdHJpbmcuaD4KCmludCBtYWluKGludCBhcmdjLCBjaGFyICoqYXJndikgewog IGNoYXIgKmN0eXBlID0gYXJndlsxXTsKICBjaGFyICptZXNzYWdlcyA9IGFyZ3ZbMl07CiAgc2V0 bG9jYWxlKExDX0NUWVBFLCBjdHlwZSk7CiAgc2V0bG9jYWxlKExDX01FU1NBR0VTLCBtZXNzYWdl cyk7CgogIHByaW50ZigiTENfQ1RZUEUgc2V0IHRvOiAlc1xuIiwgc2V0bG9jYWxlKExDX0NUWVBF LCBOVUxMKSk7CiAgcHJpbnRmKCJMQ19NRVNTQUdFUyBzZXQgdG86ICVzXG4iLCBzZXRsb2NhbGUo TENfTUVTU0FHRVMsIE5VTEwpKTsKCiAgLyogRUlMU0VROiBpbGxlZ2FsIGJ5dGUgc2VxdWVuY2Ug Ki8KICBwcmludGYoIkVycm9yIG1lc3NhZ2UgKGZyb20gc3RyZXJyb3IoRUlMU0VRKSk6ICVzXG4i LCBzdHJlcnJvcihFSUxTRVEpKTsKICByZXR1cm4gMDsKfQo= --=-DpMW8zU8xd0rsNr9YhLc--