Add Defect check for arguments `pbytes` and `nbytes`. (#128)

* Add Defect check for arguments `pbytes` and `nbytes`.

* Allow some edge cases to be not Defect.
This commit is contained in:
Eugene Kabanov 2020-09-10 03:50:06 +03:00 committed by GitHub
parent 126ea4bc56
commit 57ebe84d17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 1 deletions

View File

@ -289,6 +289,12 @@ proc readExactly*(rstream: AsyncStreamReader, pbytes: pointer,
## ##
## If EOF is received and ``nbytes`` is not yet readed, the procedure ## If EOF is received and ``nbytes`` is not yet readed, the procedure
## will raise ``AsyncStreamIncompleteError``. ## 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(): if not rstream.running():
raise newAsyncStreamIncorrectError("Incorrect stream state") 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 ## If internal buffer is not empty, ``nbytes`` bytes will be transferred from
## internal buffer, otherwise it will wait until some bytes will be received. ## 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(): if not rstream.running():
raise newAsyncStreamIncorrectError("Incorrect stream state") raise newAsyncStreamIncorrectError("Incorrect stream state")
@ -374,6 +383,13 @@ proc readUntil*(rstream: AsyncStreamReader, pbytes: pointer, nbytes: int,
## will raise ``AsyncStreamLimitError``. ## will raise ``AsyncStreamLimitError``.
## ##
## Procedure returns actual number of bytes read. ## 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(): if not rstream.running():
raise newAsyncStreamIncorrectError("Incorrect stream state") raise newAsyncStreamIncorrectError("Incorrect stream state")

View File

@ -1998,11 +1998,20 @@ template readLoop(name, body: untyped): untyped =
proc readExactly*(transp: StreamTransport, pbytes: pointer, proc readExactly*(transp: StreamTransport, pbytes: pointer,
nbytes: int) {.async.} = nbytes: int) {.async.} =
## Read exactly ``nbytes`` bytes from transport ``transp`` and store it to ## 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 ## If EOF is received and ``nbytes`` is not yet readed, the procedure
## will raise ``TransportIncompleteError``, potentially with some bytes ## will raise ``TransportIncompleteError``, potentially with some bytes
## already written. ## 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 index = 0
var pbuffer = cast[ptr UncheckedArray[byte]](pbytes) var pbuffer = cast[ptr UncheckedArray[byte]](pbytes)
readLoop("stream.transport.readExactly"): 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 ## If internal buffer is not empty, ``nbytes`` bytes will be transferred from
## internal buffer, otherwise it will wait until some bytes will be received. ## 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 var count = 0
readLoop("stream.transport.readOnce"): readLoop("stream.transport.readOnce"):
if transp.offset == 0: if transp.offset == 0:
@ -2045,6 +2057,13 @@ proc readUntil*(transp: StreamTransport, pbytes: pointer, nbytes: int,
## will raise ``TransportLimitError``. ## will raise ``TransportLimitError``.
## ##
## Procedure returns actual number of bytes read. ## 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 pbuffer = cast[ptr UncheckedArray[byte]](pbytes)
var state = 0 var state = 0
var k = 0 var k = 0