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 1v5UC6-007VtK-KX for pgsql-hackers@arkaria.postgresql.org; Sun, 05 Oct 2025 19:14:55 +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 1v5UC3-003Oqi-3B for pgsql-hackers@arkaria.postgresql.org; Sun, 05 Oct 2025 19:14:52 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1v5UC2-003Oqa-OO for pgsql-hackers@lists.postgresql.org; Sun, 05 Oct 2025 19:14:51 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1v5UC0-000YEQ-2c for pgsql-hackers@lists.postgresql.org; Sun, 05 Oct 2025 19:14:51 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 595JEi4k1768847; Sun, 5 Oct 2025 15:14:44 -0400 From: Tom Lane To: Michael Paquier cc: Ning , pgsql-hackers@lists.postgresql.org, Andrew Dunstan Subject: Re: psql client does not handle WSAEWOULDBLOCK on Windows In-reply-to: <592101.1756828301@sss.pgh.pa.us> References: <592101.1756828301@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Tue, 02 Sep 2025 11:51:41 -0400" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <1768812.1759691667.0@sss.pgh.pa.us> Date: Sun, 05 Oct 2025 15:14:44 -0400 Message-ID: <1768846.1759691684@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1768812.1759691667.1@sss.pgh.pa.us> I wrote: > Michael Paquier writes: >> Also, what about pqsecure_raw_write() in pqsecure_open_gss()? >> Shouldn't the same check apply? > Yeah, I think we pretty much need to use SOCK_ERRNO, SOCK_ERRNO_SET, > and SOCK_STRERROR (if relevant) throughout fe-secure-gssapi.c. Since nothing seems to be happening here, I took another look and decided that the required changes are really pretty straightforward. fe-secure-gssapi.c doesn't contain any strerror calls, and its touches of errno all appear to relate to socket errors, so we can just change them all. As attached. > Like you, I'm not really in a position to test this on Windows ... I'm still not, but it's straightforward enough that I'm willing to push on my own authority. I'll just put this up for long enough to make sure that the cfbot is happy with it --- though I think it's not building with GSSAPI on Windows, so that may prove little. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="v2-0001-Use-SOCK_ERRNO-_SET-in-fe-secure-gssapi.c.patch"; charset="us-ascii" Content-ID: <1768812.1759691667.2@sss.pgh.pa.us> Content-Description: v2-0001-Use-SOCK_ERRNO-_SET-in-fe-secure-gssapi.c.patch Content-Transfer-Encoding: quoted-printable =46rom 36f3ee942ef12801e52ce3fc82204455345939a6 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Sun, 5 Oct 2025 15:05:38 -0400 Subject: [PATCH v2] Use SOCK_ERRNO[_SET] in fe-secure-gssapi.c. On Windows, this code did not handle error conditions correctly at all, since it looked at "errno" which is not used for socket-related errors on that platform. This resulted, for example, in failure to connect to a PostgreSQL server with GSSAPI enabled. We have a convention for dealing with this within libpq, which is to use SOCK_ERRNO and SOCK_ERRNO_SET rather than touching errno directly; but the GSSAPI code is a relative latecomer and did not get that memo. (The equivalent backend code continues to use errno, because the backend does this differently. Maybe libpq's approach should be rethought someday.) Author: Ning Wu Co-authored-by: Tom Lane Discussion: https://postgr.es/m/CAFGqpvg-pRw=3DcdsUpKYfwY6D3d-m9tw8WMcAEE7= HHWfm-oYWvw@mail.gmail.com Backpatch-through: 13 --- src/interfaces/libpq/fe-secure-gssapi.c | 27 ++++++++++++++----------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/interfaces/libpq/fe-secure-gssapi.c b/src/interfaces/libp= q/fe-secure-gssapi.c index bc9e1ce06fa..843b31e175f 100644 --- a/src/interfaces/libpq/fe-secure-gssapi.c +++ b/src/interfaces/libpq/fe-secure-gssapi.c @@ -121,7 +121,7 @@ pg_GSS_write(PGconn *conn, const void *ptr, size_t len= ) { appendPQExpBufferStr(&conn->errorMessage, "GSSAPI caller failed to retransmit all data needing to be retrie= d\n"); - errno =3D EINVAL; + SOCK_ERRNO_SET(EINVAL); return -1; } = @@ -199,14 +199,14 @@ pg_GSS_write(PGconn *conn, const void *ptr, size_t l= en) if (major !=3D GSS_S_COMPLETE) { pg_GSS_error(libpq_gettext("GSSAPI wrap error"), conn, major, minor); - errno =3D EIO; /* for lack of a better idea */ + SOCK_ERRNO_SET(EIO); /* for lack of a better idea */ goto cleanup; } = if (conf_state =3D=3D 0) { libpq_append_conn_error(conn, "outgoing GSSAPI message would not use c= onfidentiality"); - errno =3D EIO; /* for lack of a better idea */ + SOCK_ERRNO_SET(EIO); /* for lack of a better idea */ goto cleanup; } = @@ -215,7 +215,7 @@ pg_GSS_write(PGconn *conn, const void *ptr, size_t len= ) libpq_append_conn_error(conn, "client tried to send oversize GSSAPI pa= cket (%zu > %zu)", (size_t) output.length, PQ_GSS_MAX_PACKET_SIZE - sizeof(uint32)); - errno =3D EIO; /* for lack of a better idea */ + SOCK_ERRNO_SET(EIO); /* for lack of a better idea */ goto cleanup; } = @@ -341,7 +341,7 @@ pg_GSS_read(PGconn *conn, void *ptr, size_t len) /* If we still haven't got the length, return to the caller */ if (PqGSSRecvLength < sizeof(uint32)) { - errno =3D EWOULDBLOCK; + SOCK_ERRNO_SET(EWOULDBLOCK); return -1; } } @@ -354,7 +354,7 @@ pg_GSS_read(PGconn *conn, void *ptr, size_t len) libpq_append_conn_error(conn, "oversize GSSAPI packet sent by the serv= er (%zu > %zu)", (size_t) input.length, PQ_GSS_MAX_PACKET_SIZE - sizeof(uint32)); - errno =3D EIO; /* for lack of a better idea */ + SOCK_ERRNO_SET(EIO); /* for lack of a better idea */ return -1; } = @@ -373,7 +373,7 @@ pg_GSS_read(PGconn *conn, void *ptr, size_t len) /* If we don't yet have the whole packet, return to the caller */ if (PqGSSRecvLength - sizeof(uint32) < input.length) { - errno =3D EWOULDBLOCK; + SOCK_ERRNO_SET(EWOULDBLOCK); return -1; } = @@ -393,7 +393,7 @@ pg_GSS_read(PGconn *conn, void *ptr, size_t len) pg_GSS_error(libpq_gettext("GSSAPI unwrap error"), conn, major, minor); ret =3D -1; - errno =3D EIO; /* for lack of a better idea */ + SOCK_ERRNO_SET(EIO); /* for lack of a better idea */ goto cleanup; } = @@ -401,7 +401,7 @@ pg_GSS_read(PGconn *conn, void *ptr, size_t len) { libpq_append_conn_error(conn, "incoming GSSAPI message did not use con= fidentiality"); ret =3D -1; - errno =3D EIO; /* for lack of a better idea */ + SOCK_ERRNO_SET(EIO); /* for lack of a better idea */ goto cleanup; } = @@ -437,7 +437,8 @@ gss_read(PGconn *conn, void *recv_buffer, size_t lengt= h, ssize_t *ret) *ret =3D pqsecure_raw_read(conn, recv_buffer, length); if (*ret < 0) { - if (errno =3D=3D EAGAIN || errno =3D=3D EWOULDBLOCK || errno =3D=3D EIN= TR) + if (SOCK_ERRNO =3D=3D EAGAIN || SOCK_ERRNO =3D=3D EWOULDBLOCK || + SOCK_ERRNO =3D=3D EINTR) return PGRES_POLLING_READING; else return PGRES_POLLING_FAILED; @@ -457,7 +458,8 @@ gss_read(PGconn *conn, void *recv_buffer, size_t lengt= h, ssize_t *ret) *ret =3D pqsecure_raw_read(conn, recv_buffer, length); if (*ret < 0) { - if (errno =3D=3D EAGAIN || errno =3D=3D EWOULDBLOCK || errno =3D=3D EI= NTR) + if (SOCK_ERRNO =3D=3D EAGAIN || SOCK_ERRNO =3D=3D EWOULDBLOCK || + SOCK_ERRNO =3D=3D EINTR) return PGRES_POLLING_READING; else return PGRES_POLLING_FAILED; @@ -520,7 +522,8 @@ pqsecure_open_gss(PGconn *conn) ret =3D pqsecure_raw_write(conn, PqGSSSendBuffer + PqGSSSendNext, amoun= t); if (ret < 0) { - if (errno =3D=3D EAGAIN || errno =3D=3D EWOULDBLOCK || errno =3D=3D EI= NTR) + if (SOCK_ERRNO =3D=3D EAGAIN || SOCK_ERRNO =3D=3D EWOULDBLOCK || + SOCK_ERRNO =3D=3D EINTR) return PGRES_POLLING_WRITING; else return PGRES_POLLING_FAILED; -- = 2.43.7 ------- =_aaaaaaaaaa0--