From 5f003855e7f05bd03c5531a58838540f62b8b19e Mon Sep 17 00:00:00 2001 From: Heikki Linnakangas Date: Tue, 11 Aug 2026 21:18:45 +0300 Subject: [PATCH] Fix pg_strupper/lower/title/fold() functions to work with C locale MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Starting with commit 011384ba45, calling to_date() or to_timestamp() with "TMMonth" or other TM-prefixed format keyword, with the C locale, would crash. In REL_19_STABLE and above, pg_strupper(), pg_strlower(), pg_strtitle(), and pg_strfold() functions have a special case for the C locale, but that was missing in REL_18_STABLE. On REL_18_STABLE, the functions call the libc function even in C locale, even though the native locale object is NULL. On Linux, the underlying libc functions will crash when called with NULL locale. (On macOS, they reportedly do not, but even then it's not clear if they will do what you'd expect.) This went unnoticed because until commit 011384ba45, we never called these functions in C locale, all the callers had a special codepath for C locale. We could add a special path in the new callers too, but it's an accident waiting to happen, so let's backport the C locale-specific handling from REL_19_STABLE to REL_18_STABLE. Older versions did not have these functions at all, hence no problem. This applies to REL_18_STABLE only. Reported-by: Masashi Kamura Reviewed-by: Álvaro Herrera Discussion: https://www.postgresql.org/message-id/OS9PR01MB1317436E07D06281AD1A0452F94DD2%40OS9PR01MB13174.jpnprd01.prod.outlook.com --- src/backend/utils/adt/pg_locale.c | 68 +++++++++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 4 deletions(-) diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c index 78243b2c795..2f8d5fea8f2 100644 --- a/src/backend/utils/adt/pg_locale.c +++ b/src/backend/utils/adt/pg_locale.c @@ -1273,11 +1273,64 @@ get_collation_actual_version(char collprovider, const char *collcollate) return collversion; } +/* lowercasing/casefolding in C locale */ +static size_t +strlower_c(char *dst, size_t dstsize, const char *src, size_t srclen) +{ + int i; + + for (i = 0; i < srclen && i < dstsize; i++) + dst[i] = pg_ascii_tolower(src[i]); + if (i < dstsize) + dst[i] = '\0'; + return srclen; +} + +/* titlecasing in C locale */ +static size_t +strtitle_c(char *dst, size_t dstsize, const char *src, size_t srclen) +{ + bool wasalnum = false; + int i; + + for (i = 0; i < srclen && i < dstsize; i++) + { + char c = src[i]; + + if (wasalnum) + dst[i] = pg_ascii_tolower(c); + else + dst[i] = pg_ascii_toupper(c); + + wasalnum = ((c >= '0' && c <= '9') || + (c >= 'A' && c <= 'Z') || + (c >= 'a' && c <= 'z')); + } + if (i < dstsize) + dst[i] = '\0'; + return srclen; +} + +/* uppercasing in C locale */ +static size_t +strupper_c(char *dst, size_t dstsize, const char *src, size_t srclen) +{ + int i; + + for (i = 0; i < srclen && i < dstsize; i++) + dst[i] = pg_ascii_toupper(src[i]); + if (i < dstsize) + dst[i] = '\0'; + return srclen; +} + size_t pg_strlower(char *dst, size_t dstsize, const char *src, ssize_t srclen, pg_locale_t locale) { - if (locale->provider == COLLPROVIDER_BUILTIN) + if (locale->ctype_is_c) + return strlower_c(dst, dstsize, src, srclen); + else if (locale->provider == COLLPROVIDER_BUILTIN) return strlower_builtin(dst, dstsize, src, srclen, locale); #ifdef USE_ICU else if (locale->provider == COLLPROVIDER_ICU) @@ -1296,7 +1349,9 @@ size_t pg_strtitle(char *dst, size_t dstsize, const char *src, ssize_t srclen, pg_locale_t locale) { - if (locale->provider == COLLPROVIDER_BUILTIN) + if (locale->ctype_is_c) + return strtitle_c(dst, dstsize, src, srclen); + else if (locale->provider == COLLPROVIDER_BUILTIN) return strtitle_builtin(dst, dstsize, src, srclen, locale); #ifdef USE_ICU else if (locale->provider == COLLPROVIDER_ICU) @@ -1315,7 +1370,9 @@ size_t pg_strupper(char *dst, size_t dstsize, const char *src, ssize_t srclen, pg_locale_t locale) { - if (locale->provider == COLLPROVIDER_BUILTIN) + if (locale->ctype_is_c) + return strupper_c(dst, dstsize, src, srclen); + else if (locale->provider == COLLPROVIDER_BUILTIN) return strupper_builtin(dst, dstsize, src, srclen, locale); #ifdef USE_ICU else if (locale->provider == COLLPROVIDER_ICU) @@ -1334,7 +1391,10 @@ size_t pg_strfold(char *dst, size_t dstsize, const char *src, ssize_t srclen, pg_locale_t locale) { - if (locale->provider == COLLPROVIDER_BUILTIN) + /* in the C locale, casefolding is the same as lowercasing */ + if (locale->ctype_is_c) + return strlower_c(dst, dstsize, src, srclen); + else if (locale->provider == COLLPROVIDER_BUILTIN) return strfold_builtin(dst, dstsize, src, srclen, locale); #ifdef USE_ICU else if (locale->provider == COLLPROVIDER_ICU)