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
parent 2339542832
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
proc adjustTimeout(pollTimeout: int, nextTimer: Option[int]): int {.inline.} =
if nextTimer.isNone():
if nextTimer.isNone() or pollTimeout == -1:
return pollTimeout
result = nextTimer.get()
if pollTimeout == -1: return
result = max(nextTimer.get(), 0)
result = min(pollTimeout, result)
proc callSoon*(cbproc: proc () {.gcsafe.}) {.gcsafe.}

View File

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

View File

@@ -44,8 +44,8 @@ elif defined(netbsd) or defined(openbsd):
when hasThreadSupport:
type
SelectorImpl[T] = object
kqFD : cint
maxFD : int
kqFD: cint
maxFD: int
changes: ptr SharedArray[KEvent]
fds: ptr SharedArray[SelectorKey[T]]
count: int
@@ -57,8 +57,8 @@ when hasThreadSupport:
else:
type
SelectorImpl[T] = object
kqFD : cint
maxFD : int
kqFD: cint
maxFD: int
changes: seq[KEvent]
fds: seq[SelectorKey[T]]
count: int
@@ -447,6 +447,8 @@ proc selectInto*[T](s: Selector[T], timeout: int,
ptv = addr tv
maxres = MAX_KQUEUE_EVENTS
verifySelectParams(timeout)
if timeout != -1:
if timeout >= 1000:
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):
maxres = len(results)
verifySelectParams(timeout)
s.withPollLock():
let count = posix.poll(addr(s.pollfds[0]), Tnfds(s.pollcnt), timeout)
if count < 0:

View File

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

View File

@@ -314,6 +314,14 @@ else:
key.events = {}
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):
include ioselects/ioselectors_epoll
elif bsdPlatform: