On Sun, Jul 9, 2023 at 6:35 PM Thomas Munro <thomas.mu...@gmail.com> wrote:
> On Sun, Jul 9, 2023 at 6:20 PM Peter Eisentraut <pe...@eisentraut.org> wrote:
> > So I don't think this code is correct.  AFAICT, there is nothing right
> > now that can possibly define HAVE_MBSTOWCS_L on Windows/MSVC.  Was that
> > the intention?
>
> Yes, that was my intention.  Windows actually doesn't have them.

Thinking about that some more...  Its _XXX implementations don't deal
with UTF-8 the way Unix-based developers would expect, and are
therefore just portability hazards, aren't they?  What would we gain
by restoring the advertisement that they are available?  Perhaps we
should go the other way completely and remove the relevant #defines
from win32_port.h, and fully confine knowledge of them to pg_locale.c?
 It knows how to deal with that.  Here is a patch trying this idea
out, with as slightly longer explanation.
From 1c04d781195266b6bc88d8a5a584a64aac07f613 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.mu...@gmail.com>
Date: Mon, 10 Jul 2023 13:41:27 +1200
Subject: [PATCH] Don't expose Windows' mbstowcs_l() and wcstombs_l().

Windows has similar functions with leading underscores, and we provided
the rename via a macro in win32_port.h.  In fact its functions are not
always good replacements for the Unix functions, since they can't deal
with UTF-8.  They are only currently used by pg_locale.c, which is
careful to redirect to other Windows routines for UTF-8.  Given that
portability hazard, it seem unlikely to be a good idea to encourage any
other code to think of these functions as being available outside
pg_locale.c.  Any code that thinks it wants these functions probably
wants our wchar2char() or char2wchar() routines instead, or it won't
actually work on Windows in UTF-8 databases.

Furthermore, some major libc implementations including glibc don't have
them (they only have the standard variants without _l), so external code
is very unlikely to require them to exist.

diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c
index 0eb764e897..61daa8190b 100644
--- a/src/backend/utils/adt/pg_locale.c
+++ b/src/backend/utils/adt/pg_locale.c
@@ -154,36 +154,41 @@ static void icu_set_collation_attributes(UCollator *collator, const char *loc,
 										 UErrorCode *status);
 #endif
 
-#ifndef WIN32
 /*
  * POSIX doesn't define _l-variants of these functions, but several systems
- * have them.  We provide our own replacements here.  For Windows, we have
- * macros in win32_port.h.
+ * have them.  We provide our own replacements here.
  */
 #ifndef HAVE_MBSTOWCS_L
 static size_t
 mbstowcs_l(wchar_t *dest, const char *src, size_t n, locale_t loc)
 {
+#ifdef WIN32
+	return _mbstowcs_l(dest, src, n, loc);
+#else
 	size_t		result;
 	locale_t	save_locale = uselocale(loc);
 
 	result = mbstowcs(dest, src, n);
 	uselocale(save_locale);
 	return result;
+#endif
 }
 #endif
 #ifndef HAVE_WCSTOMBS_L
 static size_t
 wcstombs_l(char *dest, const wchar_t *src, size_t n, locale_t loc)
 {
+#ifdef WIN32
+	return _wcstombs_l(dest, src, n, loc);
+#else
 	size_t		result;
 	locale_t	save_locale = uselocale(loc);
 
 	result = wcstombs(dest, src, n);
 	uselocale(save_locale);
 	return result;
-}
 #endif
+}
 #endif
 
 /*
diff --git a/src/include/port/win32_port.h b/src/include/port/win32_port.h
index b957d5c598..0e6d608330 100644
--- a/src/include/port/win32_port.h
+++ b/src/include/port/win32_port.h
@@ -455,8 +455,6 @@ extern int	_pglstat64(const char *name, struct stat *buf);
 #define strcoll_l _strcoll_l
 #define strxfrm_l _strxfrm_l
 #define wcscoll_l _wcscoll_l
-#define wcstombs_l _wcstombs_l
-#define mbstowcs_l _mbstowcs_l
 
 /*
  * Versions of libintl >= 0.18? try to replace setlocale() with a macro
-- 
2.41.0

Reply via email to