Skip to content

Commit

Permalink
Input: Ensure clearTimer won't compute a lower nfds value than one of…
Browse files Browse the repository at this point in the history
… our opened fds (#1685)

By making sure in openInputDevice that the top member of the array also happens to be the highest fd number.
  • Loading branch information
NiLuJe authored Nov 1, 2023
1 parent ff0f019 commit d1516bd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
10 changes: 10 additions & 0 deletions input/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,16 @@ static int openInputDevice(lua_State* L)
}
}

// NOTE: Make sure the top member has the highest fd number, for clearTimer's sake when it recomputes nfds
if (fd_idx > 0) {
int prev_fd = inputfds[fd_idx - 1];
int opened_fd = inputfds[fd_idx];
if (opened_fd < prev_fd) {
inputfds[fd_idx - 1] = opened_fd;
inputfds[fd_idx] = prev_fd;
}
}

// We're done w/ inputdevice, pop it
lua_settop(L, 0);
// Pass the fd to Lua, we might need it for FFI ioctl shenanigans
Expand Down
4 changes: 3 additions & 1 deletion input/timerfd-callbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ static int clearTimer(lua_State* L)
timerfd_list_delete_node(&timerfds, expired_node);

// Re-compute nfds...
nfds = inputfds[fd_idx - 1U] + 1; // NOTE: Assumes that we've got at least one fd open, which should always hold true.
// NOTE: Assumes that we've got at least one fd open, which should always hold true.
// NOTE: Also assumes that the top fd in the array is the one with the highest fd number, which openInputDevice makes sure of.
nfds = inputfds[fd_idx - 1U] + 1;
for (timerfd_node_t* restrict node = timerfds.head; node != NULL; node = node->next) {
if (node->fd >= nfds) {
nfds = node->fd + 1;
Expand Down

0 comments on commit d1516bd

Please sign in to comment.