]> git.meshlink.io Git - utcp/commitdiff
Ensure buf->offset is always smaller than buf->size.
authorGuus Sliepen <guus@sliepen.org>
Thu, 14 May 2020 21:44:38 +0000 (23:44 +0200)
committerGuus Sliepen <guus@sliepen.org>
Thu, 14 May 2020 21:44:38 +0000 (23:44 +0200)
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.

utcp.c

diff --git a/utcp.c b/utcp.c
index 47acf221318bca518eca4d88bdd7ba04b0982071..fb93f2f62f4cc5e8bbb1d122701a45009dcad131 100644 (file)
--- 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;
 
 
        uint32_t realoffset = buf->offset + offset;
 
-       if(buf->size - buf->offset < offset) {
+       if(buf->size - buf->offset <= offset) {
                // The offset wrapped
                realoffset -= buf->size;
        }
                // 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;
 
 
        uint32_t realoffset = buf->offset + offset;
 
-       if(buf->size - buf->offset < offset) {
+       if(buf->size - buf->offset <= offset) {
                // The offset wrapped
                realoffset -= buf->size;
        }
                // 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;
 
 
        uint32_t realoffset = buf->offset + offset;
 
-       if(buf->size - buf->offset < offset) {
+       if(buf->size - buf->offset <= offset) {
                // The offset wrapped
                realoffset -= buf->size;
        }
                // The offset wrapped
                realoffset -= buf->size;
        }
@@ -365,7 +365,7 @@ static ssize_t buffer_discard(struct buffer *buf, size_t len) {
                len = buf->used;
        }
 
                len = buf->used;
        }
 
-       if(buf->size - buf->offset < len) {
+       if(buf->size - buf->offset <= len) {
                buf->offset -= buf->size;
        }
 
                buf->offset -= buf->size;
        }