Skip to content

Commit 547e60b

Browse files
committed
Fix WaitEventSetWait() buffer overrun.
The WAIT_USE_EPOLL and WAIT_USE_KQUEUE implementations of WaitEventSetWaitBlock() confused the size of their internal buffer with the size of the caller's output buffer, and could ask the kernel for too many events. In fact the set of events retrieved from the kernel needs to be able to fit in both buffers, so take the smaller of the two. The WAIT_USE_POLL and WAIT_USE WIN32 implementations didn't have this confusion. This probably didn't come up before because we always used the same number in both places, but commit 7389aad calculates a dynamic size at construction time, while using MAXLISTEN for its output event buffer on the stack. That seems like a reasonable thing to want to do, so consider this to be a pre-existing bug worth fixing. As discovered by valgrind on skink. Back-patch to all supported releases for epoll, and to release 13 for the kqueue part, which copied the incorrect epoll code. Reviewed-by: Andres Freund <andres@anarazel.de> Discussion: https://postgr.es/m/901504.1673504836%40sss.pgh.pa.us
1 parent 0d9221f commit 547e60b

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/backend/storage/ipc/latch.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,7 +1469,7 @@ WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
14691469

14701470
/* Sleep */
14711471
rc = epoll_wait(set->epoll_fd, set->epoll_ret_events,
1472-
nevents, cur_timeout);
1472+
Min(nevents, set->nevents_space), cur_timeout);
14731473

14741474
/* Check return code */
14751475
if (rc < 0)
@@ -1620,7 +1620,8 @@ WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
16201620

16211621
/* Sleep */
16221622
rc = kevent(set->kqueue_fd, NULL, 0,
1623-
set->kqueue_ret_events, nevents,
1623+
set->kqueue_ret_events,
1624+
Min(nevents, set->nevents_space),
16241625
timeout_p);
16251626

16261627
/* Check return code */

0 commit comments

Comments
 (0)