From 8e993bff5326b00ced137c837fce7cd1e0ecae14 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Fri, 28 Mar 2025 16:15:57 +0100 Subject: Tidy up locale thread safety in ECPG library. Remove setlocale() and _configthreadlocal() as fallback strategy on systems that don't have uselocale(), where ECPG tries to control LC_NUMERIC formatting on input and output of floating point numbers. It was probably broken on some systems (NetBSD), and the code was also quite messy and complicated, with obsolete configure tests (Windows). It was also arguably broken, or at least had unstated environmental requirements, if pgtypeslib code was called directly. Instead, introduce PG_C_LOCALE to refer to the "C" locale as a locale_t value. It maps to the special constant LC_C_LOCALE when defined by libc (macOS, NetBSD), or otherwise uses a process-lifetime locale_t that is allocated on first use, just as ECPG previously did itself. The new replacement might be more widely useful. Then change the float parsing and printing code to pass that to _l() functions where appropriate. Unfortunately the portability of those functions is a bit complicated. First, many obvious and useful _l() functions are missing from POSIX, though most standard libraries define some of them anyway. Second, although the thread-safe save/restore technique can be used to replace the missing ones, Windows and NetBSD refused to implement standard uselocale(). They might have a point: "wide scope" uselocale() is hard to combine with other code and error-prone, especially in library code. Luckily they have the _l() functions we want so far anyway. So we have to be prepared for both ways of doing things: 1. In ECPG, use strtod_l() for parsing, and supply a port.h replacement using uselocale() over a limited scope if missing. 2. Inside our own snprintf.c, use three different approaches to format floats. For frontend code, call libc's snprintf_l(), or wrap libc's snprintf() in uselocale() if it's missing. For backend code, snprintf.c can keep assuming that the global locale's LC_NUMERIC is "C" and call libc's snprintf() without change, for now. (It might eventually be possible to call our in-tree Ryƫ routines to display floats in snprintf.c, given the C-locale-always remit of our in-tree snprintf(), but this patch doesn't risk changing anything that complicated.) Author: Thomas Munro Reviewed-by: Peter Eisentraut Reviewed-by: Tristan Partin Reviewed-by: Heikki Linnakangas Discussion: https://postgr.es/m/CWZBBRR6YA8D.8EHMDRGLCKCD%40neon.tech --- src/port/meson.build | 1 + 1 file changed, 1 insertion(+) (limited to 'src/port/meson.build') diff --git a/src/port/meson.build b/src/port/meson.build index 653539ba5b3..06809692ad0 100644 --- a/src/port/meson.build +++ b/src/port/meson.build @@ -4,6 +4,7 @@ pgport_sources = [ 'bsearch_arg.c', 'chklocale.c', 'inet_net_ntop.c', + 'locale.c', 'noblock.c', 'path.c', 'pg_bitutils.c', -- cgit v1.2.3