Skip to content

Commit 3d9c034

Browse files
committed
oauth: Ensure unused socket registrations are removed
If Curl needs to switch the direction of a socket's registration (e.g. from CURL_POLL_IN to CURL_POLL_OUT), it expects the old registration to be discarded. For epoll, this happened via EPOLL_CTL_MOD, but for kqueue, the old registration would remain if it was not explicitly removed by Curl. Explicitly remove the opposite-direction event during registrations. (If that event doesn't exist, we'll just get an ENOENT, which will be ignored by the same code that handles CURL_POLL_REMOVE.) A few assertions are also added to strengthen the relationship between the number of events added, the number of events pulled off the queue, and the lengths of the kevent arrays. Reviewed-by: Thomas Munro <thomas.munro@gmail.com> Backpatch-through: 18 Discussion: https://postgr.es/m/CAOYmi+nDZxJHaWj9_jRSyf8uMToCADAmOfJEggsKW-kY7aUwHA@mail.gmail.com
1 parent ff5b082 commit 3d9c034

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/interfaces/libpq-oauth/oauth-curl.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,22 +1291,31 @@ register_socket(CURL *curl, curl_socket_t socket, int what, void *ctx,
12911291

12921292
return 0;
12931293
#elif defined(HAVE_SYS_EVENT_H)
1294-
struct kevent ev[2] = {0};
1294+
struct kevent ev[2];
12951295
struct kevent ev_out[2];
12961296
struct timespec timeout = {0};
12971297
int nev = 0;
12981298
int res;
12991299

1300+
/*
1301+
* We don't know which of the events is currently registered, perhaps
1302+
* both, so we always try to remove unneeded events. This means we need to
1303+
* tolerate ENOENT below.
1304+
*/
13001305
switch (what)
13011306
{
13021307
case CURL_POLL_IN:
13031308
EV_SET(&ev[nev], socket, EVFILT_READ, EV_ADD | EV_RECEIPT, 0, 0, 0);
13041309
nev++;
1310+
EV_SET(&ev[nev], socket, EVFILT_WRITE, EV_DELETE | EV_RECEIPT, 0, 0, 0);
1311+
nev++;
13051312
break;
13061313

13071314
case CURL_POLL_OUT:
13081315
EV_SET(&ev[nev], socket, EVFILT_WRITE, EV_ADD | EV_RECEIPT, 0, 0, 0);
13091316
nev++;
1317+
EV_SET(&ev[nev], socket, EVFILT_READ, EV_DELETE | EV_RECEIPT, 0, 0, 0);
1318+
nev++;
13101319
break;
13111320

13121321
case CURL_POLL_INOUT:
@@ -1317,12 +1326,6 @@ register_socket(CURL *curl, curl_socket_t socket, int what, void *ctx,
13171326
break;
13181327

13191328
case CURL_POLL_REMOVE:
1320-
1321-
/*
1322-
* We don't know which of these is currently registered, perhaps
1323-
* both, so we try to remove both. This means we need to tolerate
1324-
* ENOENT below.
1325-
*/
13261329
EV_SET(&ev[nev], socket, EVFILT_READ, EV_DELETE | EV_RECEIPT, 0, 0, 0);
13271330
nev++;
13281331
EV_SET(&ev[nev], socket, EVFILT_WRITE, EV_DELETE | EV_RECEIPT, 0, 0, 0);
@@ -1334,7 +1337,10 @@ register_socket(CURL *curl, curl_socket_t socket, int what, void *ctx,
13341337
return -1;
13351338
}
13361339

1337-
res = kevent(actx->mux, ev, nev, ev_out, lengthof(ev_out), &timeout);
1340+
Assert(nev <= lengthof(ev));
1341+
Assert(nev <= lengthof(ev_out));
1342+
1343+
res = kevent(actx->mux, ev, nev, ev_out, nev, &timeout);
13381344
if (res < 0)
13391345
{
13401346
actx_error(actx, "could not modify kqueue: %m");

0 commit comments

Comments
 (0)