Skip to content

Input: Ensure clearTimer won't compute a lower nfds value than one of our opened fds #1685

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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