From: Guus Sliepen Date: Thu, 14 May 2020 21:44:38 +0000 (+0200) Subject: Ensure buf->offset is always smaller than buf->size. X-Git-Url: http://git.meshlink.io/?p=utcp;a=commitdiff_plain;h=a4a40a3d8b3b8e62853823afa34e6f4a0b40c1de Ensure buf->offset is always smaller than buf->size. Before we allowed buf->offset to be equal to buf->size. This caused an issue where buffer_call() would call the callback twice, once for 0 bytes at the end of the buffer, and once for len bytes at the start of the buffer. This would cause the callback function to think the channel had encountered an error. --- diff --git a/utcp.c b/utcp.c index 47acf22..fb93f2f 100644 --- a/utcp.c +++ b/utcp.c @@ -268,7 +268,7 @@ static ssize_t buffer_put_at(struct buffer *buf, size_t offset, const void *data uint32_t realoffset = buf->offset + offset; - if(buf->size - buf->offset < offset) { + if(buf->size - buf->offset <= offset) { // The offset wrapped realoffset -= buf->size; } @@ -305,7 +305,7 @@ static ssize_t buffer_copy(struct buffer *buf, void *data, size_t offset, size_t uint32_t realoffset = buf->offset + offset; - if(buf->size - buf->offset < offset) { + if(buf->size - buf->offset <= offset) { // The offset wrapped realoffset -= buf->size; } @@ -334,7 +334,7 @@ static ssize_t buffer_call(struct buffer *buf, utcp_recv_t cb, void *arg, size_t uint32_t realoffset = buf->offset + offset; - if(buf->size - buf->offset < offset) { + if(buf->size - buf->offset <= offset) { // The offset wrapped realoffset -= buf->size; } @@ -365,7 +365,7 @@ static ssize_t buffer_discard(struct buffer *buf, size_t len) { len = buf->used; } - if(buf->size - buf->offset < len) { + if(buf->size - buf->offset <= len) { buf->offset -= buf->size; }