Fixes crashes when asyncdispatch.adjustTimeout returns a negative value. (#11231)

This commit is contained in:
Dominik Picheta 2019-05-15 17:15:10 +01:00 • committed by Andreas Rumpf
commit d0b8724aa2
6 changed files with 22 additions and 7 deletions

View file

@ -211,11 +211,10 @@ proc processPendingCallbacks(p: PDispatcherBase; didSomeWork: var bool) =
didSomeWork = true didSomeWork = true
proc adjustTimeout(pollTimeout: int, nextTimer: Option[int]): int {.inline.} = proc adjustTimeout(pollTimeout: int, nextTimer: Option[int]): int {.inline.} =
if nextTimer.isNone(): if nextTimer.isNone() or pollTimeout == -1:
return pollTimeout return pollTimeout
result = nextTimer.get() result = max(nextTimer.get(), 0)
if pollTimeout == -1: return
result = min(pollTimeout, result) result = min(pollTimeout, result)
proc callSoon*(cbproc: proc () {.gcsafe.}) {.gcsafe.} proc callSoon*(cbproc: proc () {.gcsafe.}) {.gcsafe.}

View file

@ -378,6 +378,8 @@ proc selectInto*[T](s: Selector[T], timeout: int,
if maxres > len(results): if maxres > len(results):
maxres = len(results) maxres = len(results)
verifySelectParams(timeout)
let count = epoll_wait(s.epollFD, addr(resTable[0]), maxres.cint, let count = epoll_wait(s.epollFD, addr(resTable[0]), maxres.cint,
timeout.cint) timeout.cint)
if count < 0: if count < 0:

View file

@ -44,8 +44,8 @@ elif defined(netbsd) or defined(openbsd):
when hasThreadSupport: when hasThreadSupport:
type type
SelectorImpl[T] = object SelectorImpl[T] = object
kqFD : cint kqFD: cint
maxFD : int maxFD: int
changes: ptr SharedArray[KEvent] changes: ptr SharedArray[KEvent]
fds: ptr SharedArray[SelectorKey[T]] fds: ptr SharedArray[SelectorKey[T]]
count: int count: int
@ -57,8 +57,8 @@ when hasThreadSupport:
else: else:
type type
SelectorImpl[T] = object SelectorImpl[T] = object
kqFD : cint kqFD: cint
maxFD : int maxFD: int
changes: seq[KEvent] changes: seq[KEvent]
fds: seq[SelectorKey[T]] fds: seq[SelectorKey[T]]
count: int count: int
@ -447,6 +447,8 @@ proc selectInto*[T](s: Selector[T], timeout: int,
ptv = addr tv ptv = addr tv
maxres = MAX_KQUEUE_EVENTS maxres = MAX_KQUEUE_EVENTS
verifySelectParams(timeout)
if timeout != -1: if timeout != -1:
if timeout >= 1000: if timeout >= 1000:
tv.tv_sec = posix.Time(timeout div 1_000) tv.tv_sec = posix.Time(timeout div 1_000)

View file

@ -214,6 +214,8 @@ proc selectInto*[T](s: Selector[T], timeout: int,
if maxres > len(results): if maxres > len(results):
maxres = len(results) maxres = len(results)
verifySelectParams(timeout)
s.withPollLock(): s.withPollLock():
let count = posix.poll(addr(s.pollfds[0]), Tnfds(s.pollcnt), timeout) let count = posix.poll(addr(s.pollfds[0]), Tnfds(s.pollcnt), timeout)
if count < 0: if count < 0:

View file

@ -309,6 +309,8 @@ proc selectInto*[T](s: Selector[T], timeout: int,
var ptv = addr tv var ptv = addr tv
var rset, wset, eset: FdSet var rset, wset, eset: FdSet
verifySelectParams(timeout)
if timeout != -1: if timeout != -1:
when defined(genode): when defined(genode):
tv.tv_sec = Time(timeout div 1_000) tv.tv_sec = Time(timeout div 1_000)

View file

@ -314,6 +314,14 @@ else:
key.events = {} key.events = {}
key.data = empty key.data = empty
proc verifySelectParams(timeout: int) =
# Timeout of -1 means: wait forever
# Anything higher is the time to wait in miliseconds.
if timeout < -1:
raise newException(
ValueError, "Cannot select with a negative value, got " & $timeout
)
when defined(linux): when defined(linux):
include ioselects/ioselectors_epoll include ioselects/ioselectors_epoll
elif bsdPlatform: elif bsdPlatform: