From 12f0b5b302707e30e7796109eade9bfb1a414212 Mon Sep 17 00:00:00 2001
From: Ning Wu <ning94803@gmail.com>
Date: Tue, 30 Jul 2024 11:27:00 +0800
Subject: [PATCH] Fix error handling for non-blocking socket on Windows

The connection fails with a non-blocking socket error when using psql on
Windows to connect to a PostgreSQL server with GSSAPI enabled.

On Windows, the socket error code is obtained by WSAGetLastError() instead of
errno. This causes the value of errno to be incorrect when handling with
non-blocking socket error.

By using the SOCK_ERRNO macro to ensure that all platforms can get the error
code correctly.

Authored-by: Ning Wu <ning94803@gmail.com>
---
 src/interfaces/libpq/fe-secure-gssapi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/interfaces/libpq/fe-secure-gssapi.c b/src/interfaces/libpq/fe-secure-gssapi.c
index 98b314613c..8c81efe47b 100644
--- a/src/interfaces/libpq/fe-secure-gssapi.c
+++ b/src/interfaces/libpq/fe-secure-gssapi.c
@@ -430,7 +430,7 @@ gss_read(PGconn *conn, void *recv_buffer, size_t length, ssize_t *ret)
 	*ret = pqsecure_raw_read(conn, recv_buffer, length);
 	if (*ret < 0)
 	{
-		if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
+		if (SOCK_ERRNO == EAGAIN || SOCK_ERRNO == EWOULDBLOCK || SOCK_ERRNO == EINTR)
 			return PGRES_POLLING_READING;
 		else
 			return PGRES_POLLING_FAILED;
-- 
2.44.0

