From bdba003a7ff21aca3051693e9ffb0b67141799f2 Mon Sep 17 00:00:00 2001 From: shihao zhong Date: Thu, 15 Oct 2026 22:51:23 -0400 Subject: [PATCH v8] psql: Fall back to English for unsafe hidden query labels. Since 41d69e6dcca the labels are translated, and the server rejects the query when the translation is not valid in its encoding. Translate the label only when the client and server encodings are both UTF8 and the translated bytes are valid UTF8. --- src/bin/psql/common.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index 788555b9782..50687a325d2 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -28,6 +28,7 @@ #include "fe_utils/mbprint.h" #include "fe_utils/string_utils.h" #include "mainloop.h" +#include "mb/pg_wchar.h" #include "portability/instr_time.h" #include "settings.h" @@ -2630,9 +2631,27 @@ command_no_begin(const char *query) void addQueryComment(PQExpBuffer buf, const char *label) { -#if defined(ENABLE_NLS) - /* This will become conditional once we've settled on the tests to use */ - label = _(label); +#if defined(ENABLE_NLS) && !defined(WIN32) + const char *server_enc = PQparameterStatus(pset.db, "server_encoding"); + int client_enc = PQclientEncoding(pset.db); + + /* + * Use the translation only if client and server both use UTF8 and the + * translated bytes are valid UTF8. Then the server accepts the query and + * the label is valid text in the database. Single-byte encodings accept + * almost any byte, so the same check proves little there. Don't try on + * Windows, where the encoding gettext produces is harder to predict. + */ + if (server_enc != NULL && + pg_char_to_encoding(server_enc) == PG_UTF8 && + client_enc == PG_UTF8) + { + const char *translated = _(label); + int len = strlen(translated); + + if (pg_encoding_verifymbstr(PG_UTF8, translated, len) == len) + label = translated; + } #endif printfPQExpBuffer(buf, "/* %s */\n", label); -- 2.37.1 (Apple Git-137.1)