🌐 AI搜索 & 代理 主页
Skip to content

Commit cc82448

Browse files
committed
libpq-fe.h: Don't claim SOCKTYPE in the global namespace
The definition of PGoauthBearerRequest uses a temporary SOCKTYPE macro to hide the difference between Windows and Berkeley socket handles, since we don't surface pgsocket in our public API. This macro doesn't need to escape the header, because implementers will choose the correct socket type based on their platform, so I #undef'd it immediately after use. I didn't namespace that helper, though, so if anyone else needs a SOCKTYPE macro, libpq-fe.h will now unhelpfully get rid of it. This doesn't seem too far-fetched, given its proximity to existing POSIX macro names. Add a PQ_ prefix to avoid collisions, update and improve the surrounding documentation, and backpatch. Reviewed-by: Chao Li <li.evan.chao@gmail.com> Discussion: https://postgr.es/m/CAOYmi%2BmrGg%2Bn_X2MOLgeWcj3v_M00gR8uz_D7mM8z%3DdX1JYVbg%40mail.gmail.com Backpatch-through: 18
1 parent c8098aa commit cc82448

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

doc/src/sgml/libpq.sgml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10412,10 +10412,14 @@ typedef struct PGoauthBearerRequest
1041210412

1041310413
/* Hook outputs */
1041410414

10415-
/* Callback implementing a custom asynchronous OAuth flow. */
10415+
/*
10416+
* Callback implementing a custom asynchronous OAuth flow. The signature is
10417+
* platform-dependent: PQ_SOCKTYPE is SOCKET on Windows, and int everywhere
10418+
* else.
10419+
*/
1041610420
PostgresPollingStatusType (*async) (PGconn *conn,
1041710421
struct PGoauthBearerRequest *request,
10418-
SOCKTYPE *altsock);
10422+
PQ_SOCKTYPE *altsock);
1041910423

1042010424
/* Callback to clean up custom allocations. */
1042110425
void (*cleanup) (PGconn *conn, struct PGoauthBearerRequest *request);
@@ -10472,7 +10476,7 @@ typedef struct PGoauthBearerRequest
1047210476
hook. When the callback cannot make further progress without blocking,
1047310477
it should return either <symbol>PGRES_POLLING_READING</symbol> or
1047410478
<symbol>PGRES_POLLING_WRITING</symbol> after setting
10475-
<literal>*pgsocket</literal> to the file descriptor that will be marked
10479+
<literal>*altsock</literal> to the file descriptor that will be marked
1047610480
ready to read/write when progress can be made again. (This descriptor
1047710481
is then provided to the top-level polling loop via
1047810482
<function>PQsocket()</function>.) Return <symbol>PGRES_POLLING_OK</symbol>

src/interfaces/libpq/libpq-fe.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -738,11 +738,15 @@ typedef struct _PGpromptOAuthDevice
738738
int expires_in; /* seconds until user code expires */
739739
} PGpromptOAuthDevice;
740740

741-
/* for PGoauthBearerRequest.async() */
741+
/*
742+
* For PGoauthBearerRequest.async(). This macro just allows clients to avoid
743+
* depending on libpq-int.h or Winsock for the "socket" type; it's undefined
744+
* immediately below.
745+
*/
742746
#ifdef _WIN32
743-
#define SOCKTYPE uintptr_t /* avoids depending on winsock2.h for SOCKET */
747+
#define PQ_SOCKTYPE uintptr_t /* avoids depending on winsock2.h for SOCKET */
744748
#else
745-
#define SOCKTYPE int
749+
#define PQ_SOCKTYPE int
746750
#endif
747751

748752
typedef struct PGoauthBearerRequest
@@ -768,10 +772,13 @@ typedef struct PGoauthBearerRequest
768772
* blocking during the original call to the PQAUTHDATA_OAUTH_BEARER_TOKEN
769773
* hook, it may be returned directly, but one of request->async or
770774
* request->token must be set by the hook.
775+
*
776+
* The (PQ_SOCKTYPE *) in the signature is a placeholder for the platform's
777+
* native socket type: (SOCKET *) on Windows, and (int *) everywhere else.
771778
*/
772779
PostgresPollingStatusType (*async) (PGconn *conn,
773780
struct PGoauthBearerRequest *request,
774-
SOCKTYPE * altsock);
781+
PQ_SOCKTYPE * altsock);
775782

776783
/*
777784
* Callback to clean up custom allocations. A hook implementation may use
@@ -798,7 +805,7 @@ typedef struct PGoauthBearerRequest
798805
void *user;
799806
} PGoauthBearerRequest;
800807

801-
#undef SOCKTYPE
808+
#undef PQ_SOCKTYPE
802809

803810
extern char *PQencryptPassword(const char *passwd, const char *user);
804811
extern char *PQencryptPasswordConn(PGconn *conn, const char *passwd, const char *user, const char *algorithm);

0 commit comments

Comments
 (0)