Skip to content

Commit 6744247

Browse files
authored
Fix broken poll and nfds_t bindings (#24331)
This fixes several cases of the Nim binding of nfds_t being inconsistent with the target platform signedness and/or size. Additionally, it fixes poll's third argument (timeout) being set to Nim "int" when it should have been "cint". The former is the same issue that #23045 had attempted to fix, but failed because it only considered Linux. (Also, it was only applied to version 2.0, so the two branches now have incompatible versions of the same bug.) Notes: * SVR4's original "unsigned long" definition is cloned by Linux and Haiku. Nim got this right for Haiku and Linux-amd64, but it was wrong on non-amd64 Linux. * Zephyr does not have nfds_t, but simply uses (signed) "int". This was already correctly reflected by Nim. * OpenBSD poll.h uses "unsigned int", and other BSD derivatives follow suit. This being the most commonly copied definition, the fallback case now returns cuint. (This also seems to be correct for the OS X headers I could find on the web.) * This changes Nintendo Switch nfds_t to cuint from culong. It is purportedly a FreeBSD derivative, so I *think* this is correct, but I can't tell because I don't have access to the Nintendo Switch headers. I have also moved the platform-specific Tnfds to posix.nim so that we can reuse the fallback logic on all platforms. (e.g. specifying the size in posix_linux_amd64 only to then use when defined(linux) in posix_other seems redundant.)
1 parent e69eb99 commit 6744247

File tree

9 files changed

+19
-22
lines changed

9 files changed

+19
-22
lines changed

lib/posix/posix.nim

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1095,7 +1095,21 @@ proc setprotoent*(a1: cint) {.importc, header: "<netdb.h>".}
10951095
proc setservent*(a1: cint) {.importc, header: "<netdb.h>".}
10961096

10971097
when not defined(lwip):
1098-
proc poll*(a1: ptr TPollfd, a2: Tnfds, a3: int): cint {.
1098+
# Linux and Haiku emulate SVR4, which used unsigned long.
1099+
# Meanwhile, BSD derivatives had used unsigned int; we will use this
1100+
# for the else case, because it is more widely cloned than SVR4's
1101+
# behavior.
1102+
when defined(linux) or defined(haiku):
1103+
type
1104+
Tnfds* {.importc: "nfds_t", header: "<poll.h>".} = culong
1105+
elif defined(zephyr):
1106+
type
1107+
Tnfds* = distinct cint
1108+
else:
1109+
type
1110+
Tnfds* {.importc: "nfds_t", header: "<poll.h>".} = cuint
1111+
1112+
proc poll*(a1: ptr TPollfd, a2: Tnfds, a3: cint): cint {.
10991113
importc, header: "<poll.h>", sideEffect.}
11001114

11011115
proc realpath*(name, resolved: cstring): cstring {.

lib/posix/posix_haiku.nim

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,8 +519,6 @@ type
519519
events*: cshort ## The input event flags (see below).
520520
revents*: cshort ## The output event flags (see below).
521521

522-
Tnfds* {.importc: "nfds_t", header: "<poll.h>".} = culong
523-
524522
var
525523
errno* {.importc, header: "<errno.h>".}: cint ## error variable
526524
h_errno* {.importc, header: "<netdb.h>".}: cint

lib/posix/posix_linux_amd64.nim

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,8 +563,6 @@ type
563563
events*: cshort ## The input event flags (see below).
564564
revents*: cshort ## The output event flags (see below).
565565

566-
Tnfds* {.importc: "nfds_t", header: "<poll.h>".} = culong
567-
568566
var
569567
errno* {.importc, header: "<errno.h>".}: cint ## error variable
570568
h_errno* {.importc, header: "<netdb.h>".}: cint

lib/posix/posix_macos_amd64.nim

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,8 +533,6 @@ type
533533
events*: cshort ## The input event flags (see below).
534534
revents*: cshort ## The output event flags (see below).
535535

536-
Tnfds* {.importc: "nfds_t", header: "<poll.h>".} = cint
537-
538536
var
539537
errno* {.importc, header: "<errno.h>".}: cint ## error variable
540538
h_errno* {.importc, header: "<netdb.h>".}: cint

lib/posix/posix_nintendoswitch.nim

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,6 @@ type
484484
events*: cshort ## The input event flags (see below).
485485
revents*: cshort ## The output event flags (see below).
486486

487-
Tnfds* {.importc: "nfds_t", header: "<poll.h>".} = culong
488-
489487
var
490488
errno* {.importc, header: "<errno.h>".}: cint ## error variable
491489
h_errno* {.importc, header: "<netdb.h>".}: cint

lib/posix/posix_openbsd_amd64.nim

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,8 +517,6 @@ type
517517
events*: cshort ## The input event flags (see below).
518518
revents*: cshort ## The output event flags (see below).
519519

520-
Tnfds* {.importc: "nfds_t", header: "<poll.h>".} = cint
521-
522520
var
523521
errno* {.importc, header: "<errno.h>".}: cint ## error variable
524522
h_errno* {.importc, header: "<netdb.h>".}: cint

lib/posix/posix_other.nim

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -604,13 +604,6 @@ when not defined(lwip):
604604
events*: cshort ## The input event flags (see below).
605605
revents*: cshort ## The output event flags (see below).
606606

607-
when defined(zephyr):
608-
type
609-
Tnfds* = distinct cint
610-
else:
611-
type
612-
Tnfds* {.importc: "nfds_t", header: "<poll.h>".} = cint
613-
614607
var
615608
errno* {.importc, header: "<errno.h>".}: cint ## error variable
616609
h_errno* {.importc, header: "<netdb.h>".}: cint

lib/pure/ioselects/ioselectors_poll.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ proc selectInto*[T](s: Selector[T], timeout: int,
231231
verifySelectParams(timeout)
232232

233233
s.withPollLock():
234-
let count = posix.poll(addr(s.pollfds[0]), Tnfds(s.pollcnt), timeout)
234+
let count = posix.poll(addr(s.pollfds[0]), Tnfds(s.pollcnt), cint(timeout))
235235
if count < 0:
236236
result = 0
237237
let err = osLastError()

lib/pure/net.nim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ when defined(nimHasStyleChecks):
211211
when defined(posix) and not defined(lwip):
212212
from std/posix import TPollfd, POLLIN, POLLPRI, POLLOUT, POLLWRBAND, Tnfds
213213

214-
template monitorPollEvent(x: var SocketHandle, y: cint, timeout: int): int =
214+
template monitorPollEvent(x: var SocketHandle, y, timeout: cint): int =
215215
var tpollfd: TPollfd
216216
tpollfd.fd = cast[cint](x)
217217
tpollfd.events = y
@@ -222,14 +222,14 @@ proc timeoutRead(fd: var SocketHandle, timeout = 500): int =
222222
var fds = @[fd]
223223
selectRead(fds, timeout)
224224
else:
225-
monitorPollEvent(fd, POLLIN or POLLPRI, timeout)
225+
monitorPollEvent(fd, POLLIN or POLLPRI, cint(timeout))
226226

227227
proc timeoutWrite(fd: var SocketHandle, timeout = 500): int =
228228
when defined(windows) or defined(lwip):
229229
var fds = @[fd]
230230
selectWrite(fds, timeout)
231231
else:
232-
monitorPollEvent(fd, POLLOUT or POLLWRBAND, timeout)
232+
monitorPollEvent(fd, POLLOUT or POLLWRBAND, cint(timeout))
233233

234234
proc socketError*(socket: Socket, err: int = -1, async = false,
235235
lastError = (-1).OSErrorCode,

0 commit comments

Comments
 (0)