Skip to content

Commit f1ef654

Browse files
tglsfdcCommitfest Bot
authored andcommitted
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 <[email protected]> Co-authored-by: Tom Lane <[email protected]> Discussion: https://postgr.es/m/CAFGqpvg-pRw=cdsUpKYfwY6D3d-m9tw8WMcAEE7HHWfm-oYWvw@mail.gmail.com Backpatch-through: 13
1 parent 1a8b5b1 commit f1ef654

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

src/interfaces/libpq/fe-secure-gssapi.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ pg_GSS_write(PGconn *conn, const void *ptr, size_t len)
121121
{
122122
appendPQExpBufferStr(&conn->errorMessage,
123123
"GSSAPI caller failed to retransmit all data needing to be retried\n");
124-
errno = EINVAL;
124+
SOCK_ERRNO_SET(EINVAL);
125125
return -1;
126126
}
127127

@@ -199,14 +199,14 @@ pg_GSS_write(PGconn *conn, const void *ptr, size_t len)
199199
if (major != GSS_S_COMPLETE)
200200
{
201201
pg_GSS_error(libpq_gettext("GSSAPI wrap error"), conn, major, minor);
202-
errno = EIO; /* for lack of a better idea */
202+
SOCK_ERRNO_SET(EIO); /* for lack of a better idea */
203203
goto cleanup;
204204
}
205205

206206
if (conf_state == 0)
207207
{
208208
libpq_append_conn_error(conn, "outgoing GSSAPI message would not use confidentiality");
209-
errno = EIO; /* for lack of a better idea */
209+
SOCK_ERRNO_SET(EIO); /* for lack of a better idea */
210210
goto cleanup;
211211
}
212212

@@ -215,7 +215,7 @@ pg_GSS_write(PGconn *conn, const void *ptr, size_t len)
215215
libpq_append_conn_error(conn, "client tried to send oversize GSSAPI packet (%zu > %zu)",
216216
(size_t) output.length,
217217
PQ_GSS_MAX_PACKET_SIZE - sizeof(uint32));
218-
errno = EIO; /* for lack of a better idea */
218+
SOCK_ERRNO_SET(EIO); /* for lack of a better idea */
219219
goto cleanup;
220220
}
221221

@@ -341,7 +341,7 @@ pg_GSS_read(PGconn *conn, void *ptr, size_t len)
341341
/* If we still haven't got the length, return to the caller */
342342
if (PqGSSRecvLength < sizeof(uint32))
343343
{
344-
errno = EWOULDBLOCK;
344+
SOCK_ERRNO_SET(EWOULDBLOCK);
345345
return -1;
346346
}
347347
}
@@ -354,7 +354,7 @@ pg_GSS_read(PGconn *conn, void *ptr, size_t len)
354354
libpq_append_conn_error(conn, "oversize GSSAPI packet sent by the server (%zu > %zu)",
355355
(size_t) input.length,
356356
PQ_GSS_MAX_PACKET_SIZE - sizeof(uint32));
357-
errno = EIO; /* for lack of a better idea */
357+
SOCK_ERRNO_SET(EIO); /* for lack of a better idea */
358358
return -1;
359359
}
360360

@@ -373,7 +373,7 @@ pg_GSS_read(PGconn *conn, void *ptr, size_t len)
373373
/* If we don't yet have the whole packet, return to the caller */
374374
if (PqGSSRecvLength - sizeof(uint32) < input.length)
375375
{
376-
errno = EWOULDBLOCK;
376+
SOCK_ERRNO_SET(EWOULDBLOCK);
377377
return -1;
378378
}
379379

@@ -393,15 +393,15 @@ pg_GSS_read(PGconn *conn, void *ptr, size_t len)
393393
pg_GSS_error(libpq_gettext("GSSAPI unwrap error"), conn,
394394
major, minor);
395395
ret = -1;
396-
errno = EIO; /* for lack of a better idea */
396+
SOCK_ERRNO_SET(EIO); /* for lack of a better idea */
397397
goto cleanup;
398398
}
399399

400400
if (conf_state == 0)
401401
{
402402
libpq_append_conn_error(conn, "incoming GSSAPI message did not use confidentiality");
403403
ret = -1;
404-
errno = EIO; /* for lack of a better idea */
404+
SOCK_ERRNO_SET(EIO); /* for lack of a better idea */
405405
goto cleanup;
406406
}
407407

@@ -437,7 +437,8 @@ gss_read(PGconn *conn, void *recv_buffer, size_t length, ssize_t *ret)
437437
*ret = pqsecure_raw_read(conn, recv_buffer, length);
438438
if (*ret < 0)
439439
{
440-
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
440+
if (SOCK_ERRNO == EAGAIN || SOCK_ERRNO == EWOULDBLOCK ||
441+
SOCK_ERRNO == EINTR)
441442
return PGRES_POLLING_READING;
442443
else
443444
return PGRES_POLLING_FAILED;
@@ -457,7 +458,8 @@ gss_read(PGconn *conn, void *recv_buffer, size_t length, ssize_t *ret)
457458
*ret = pqsecure_raw_read(conn, recv_buffer, length);
458459
if (*ret < 0)
459460
{
460-
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
461+
if (SOCK_ERRNO == EAGAIN || SOCK_ERRNO == EWOULDBLOCK ||
462+
SOCK_ERRNO == EINTR)
461463
return PGRES_POLLING_READING;
462464
else
463465
return PGRES_POLLING_FAILED;
@@ -520,7 +522,8 @@ pqsecure_open_gss(PGconn *conn)
520522
ret = pqsecure_raw_write(conn, PqGSSSendBuffer + PqGSSSendNext, amount);
521523
if (ret < 0)
522524
{
523-
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
525+
if (SOCK_ERRNO == EAGAIN || SOCK_ERRNO == EWOULDBLOCK ||
526+
SOCK_ERRNO == EINTR)
524527
return PGRES_POLLING_WRITING;
525528
else
526529
return PGRES_POLLING_FAILED;

0 commit comments

Comments
 (0)