mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-28 07:18:30 +00:00
Fix select() on Linux
This fixes a bug where the caller's timeval will be clobbered on Linux. The Kernel ABI *always* modifies the timeout argument but POSIX says it should be a const parameter. The wrapper now handles the difference and sys_select() may be used if obtaining the remainder on Linux is needed.
This commit is contained in:
parent
dfabcd84c1
commit
0f1ead8943
2 changed files with 17 additions and 6 deletions
|
@ -43,26 +43,37 @@
|
|||
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
|
||||
struct timeval *timeout) {
|
||||
int rc;
|
||||
struct timeval tv, *tvp;
|
||||
POLLTRACE("select(%d, %p, %p, %p, %s) → ...", nfds, readfds, writefds,
|
||||
exceptfds, DescribeTimeval(0, timeout));
|
||||
|
||||
// the linux kernel modifies timeout
|
||||
if (timeout) {
|
||||
if (IsAsan() && !__asan_is_valid(timeout, sizeof(*timeout))) {
|
||||
return efault();
|
||||
}
|
||||
tv = *timeout;
|
||||
tvp = &tv;
|
||||
} else {
|
||||
tvp = 0;
|
||||
}
|
||||
|
||||
BEGIN_CANCELLATION_POINT;
|
||||
if (nfds < 0) {
|
||||
rc = einval();
|
||||
} else if (IsAsan() &&
|
||||
((readfds && !__asan_is_valid(readfds, FD_SIZE(nfds))) ||
|
||||
(writefds && !__asan_is_valid(writefds, FD_SIZE(nfds))) ||
|
||||
(exceptfds && !__asan_is_valid(exceptfds, FD_SIZE(nfds))) ||
|
||||
(timeout && !__asan_is_valid(timeout, sizeof(*timeout))))) {
|
||||
(exceptfds && !__asan_is_valid(exceptfds, FD_SIZE(nfds))))) {
|
||||
rc = efault();
|
||||
} else if (!IsWindows()) {
|
||||
rc = sys_select(nfds, readfds, writefds, exceptfds, timeout);
|
||||
rc = sys_select(nfds, readfds, writefds, exceptfds, tvp);
|
||||
} else {
|
||||
rc = sys_select_nt(nfds, readfds, writefds, exceptfds, timeout, 0);
|
||||
rc = sys_select_nt(nfds, readfds, writefds, exceptfds, tvp, 0);
|
||||
}
|
||||
END_CANCELLATION_POINT;
|
||||
|
||||
POLLTRACE("select(%d, %p, %p, %p, [%s]) → %d% m", nfds, readfds, writefds,
|
||||
exceptfds, DescribeTimeval(rc, timeout), rc);
|
||||
exceptfds, DescribeTimeval(rc, tvp), rc);
|
||||
return rc;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue