Skip to content

Commit 8b217c9

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 5b4fb2b commit 8b217c9

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
@@ -10422,10 +10422,14 @@ typedef struct PGoauthBearerRequest
1042210422

1042310423
/* Hook outputs */
1042410424

10425-
/* Callback implementing a custom asynchronous OAuth flow. */
10425+
/*
10426+
* Callback implementing a custom asynchronous OAuth flow. The signature is
10427+
* platform-dependent: PQ_SOCKTYPE is SOCKET on Windows, and int everywhere
10428+
* else.
10429+
*/
1042610430
PostgresPollingStatusType (*async) (PGconn *conn,
1042710431
struct PGoauthBearerRequest *request,
10428-
SOCKTYPE *altsock);
10432+
PQ_SOCKTYPE *altsock);
1042910433

1043010434
/* Callback to clean up custom allocations. */
1043110435
void (*cleanup) (PGconn *conn, struct PGoauthBearerRequest *request);
@@ -10482,7 +10486,7 @@ typedef struct PGoauthBearerRequest
1048210486
hook. When the callback cannot make further progress without blocking,
1048310487
it should return either <symbol>PGRES_POLLING_READING</symbol> or
1048410488
<symbol>PGRES_POLLING_WRITING</symbol> after setting
10485-
<literal>*pgsocket</literal> to the file descriptor that will be marked
10489+
<literal>*altsock</literal> to the file descriptor that will be marked
1048610490
ready to read/write when progress can be made again. (This descriptor
1048710491
is then provided to the top-level polling loop via
1048810492
<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)