diff --git a/chronos/streams/asyncstream.nim b/chronos/streams/asyncstream.nim index 6fc194fa..bc1f6f94 100644 --- a/chronos/streams/asyncstream.nim +++ b/chronos/streams/asyncstream.nim @@ -289,6 +289,12 @@ proc readExactly*(rstream: AsyncStreamReader, pbytes: pointer, ## ## If EOF is received and ``nbytes`` is not yet readed, the procedure ## will raise ``AsyncStreamIncompleteError``. + doAssert(not(isNil(pbytes)), "pbytes must not be nil") + doAssert(nbytes >= 0, "nbytes must be non-negative integer") + + if nbytes == 0: + return + if not rstream.running(): raise newAsyncStreamIncorrectError("Incorrect stream state") @@ -329,6 +335,9 @@ proc readOnce*(rstream: AsyncStreamReader, pbytes: pointer, ## ## If internal buffer is not empty, ``nbytes`` bytes will be transferred from ## internal buffer, otherwise it will wait until some bytes will be received. + doAssert(not(isNil(pbytes)), "pbytes must not be nil") + doAssert(nbytes > 0, "nbytes must be positive value") + if not rstream.running(): raise newAsyncStreamIncorrectError("Incorrect stream state") @@ -374,6 +383,13 @@ proc readUntil*(rstream: AsyncStreamReader, pbytes: pointer, nbytes: int, ## will raise ``AsyncStreamLimitError``. ## ## Procedure returns actual number of bytes read. + doAssert(not(isNil(pbytes)), "pbytes must not be nil") + doAssert(len(sep) > 0, "separator must not be empty") + doAssert(nbytes >= 0, "nbytes must be non-negative value") + + if nbytes == 0: + raise newAsyncStreamLimitError() + if not rstream.running(): raise newAsyncStreamIncorrectError("Incorrect stream state") diff --git a/chronos/transports/stream.nim b/chronos/transports/stream.nim index 59d89da1..45073e11 100644 --- a/chronos/transports/stream.nim +++ b/chronos/transports/stream.nim @@ -1998,11 +1998,20 @@ template readLoop(name, body: untyped): untyped = proc readExactly*(transp: StreamTransport, pbytes: pointer, nbytes: int) {.async.} = ## Read exactly ``nbytes`` bytes from transport ``transp`` and store it to - ## ``pbytes``. + ## ``pbytes``. ``pbytes`` must not be ``nil`` pointer and ``nbytes`` should + ## be Natural. + ## + ## If ``nbytes == 0`` this operation will return immediately. ## ## If EOF is received and ``nbytes`` is not yet readed, the procedure ## will raise ``TransportIncompleteError``, potentially with some bytes ## already written. + doAssert(not(isNil(pbytes)), "pbytes must not be nil") + doAssert(nbytes >= 0, "nbytes must be non-negative integer") + + if nbytes == 0: + return + var index = 0 var pbuffer = cast[ptr UncheckedArray[byte]](pbytes) readLoop("stream.transport.readExactly"): @@ -2021,6 +2030,9 @@ proc readOnce*(transp: StreamTransport, pbytes: pointer, ## ## If internal buffer is not empty, ``nbytes`` bytes will be transferred from ## internal buffer, otherwise it will wait until some bytes will be received. + doAssert(not(isNil(pbytes)), "pbytes must not be nil") + doAssert(nbytes > 0, "nbytes must be positive integer") + var count = 0 readLoop("stream.transport.readOnce"): if transp.offset == 0: @@ -2045,6 +2057,13 @@ proc readUntil*(transp: StreamTransport, pbytes: pointer, nbytes: int, ## will raise ``TransportLimitError``. ## ## Procedure returns actual number of bytes read. + doAssert(not(isNil(pbytes)), "pbytes must not be nil") + doAssert(len(sep) > 0, "separator must not be empty") + doAssert(nbytes >= 0, "nbytes must be non-negative integer") + + if nbytes == 0: + raise newException(TransportLimitError, "Limit reached!") + var pbuffer = cast[ptr UncheckedArray[byte]](pbytes) var state = 0 var k = 0