diff options
author | Richard Braun <rbraun@sceen.net> | 2018-08-25 16:42:57 +0200 |
---|---|---|
committer | Richard Braun <rbraun@sceen.net> | 2018-08-25 16:42:57 +0200 |
commit | 9d5114c068344962725b6d39319f4e68d855227d (patch) | |
tree | 0d50a45820209a979337e1111d1becfa4f91507f | |
parent | de950994b2238c2568602dd2a7ac40e827e3d8c6 (diff) |
cbuf: make empty reads succeed
Getting an error when reading 0 bytes is surprising compared to common
read interfaces.
-rw-r--r-- | src/cbuf.c | 3 | ||||
-rw-r--r-- | src/cbuf.h | 7 | ||||
-rw-r--r-- | test/test_cbuf.c | 2 |
3 files changed, 9 insertions, 3 deletions
@@ -172,8 +172,7 @@ cbuf_read(const struct cbuf *cbuf, size_t index, void *buf, size_t *sizep) const uint8_t *start, *end, *buf_end; size_t size; - /* At least one byte must be available */ - if (!cbuf_range_valid(cbuf, index, index + 1)) { + if (!cbuf_index_valid(cbuf, index)) { return EINVAL; } @@ -83,6 +83,13 @@ cbuf_clear(struct cbuf *cbuf) } static inline bool +cbuf_index_valid(const struct cbuf *cbuf, size_t index) +{ + return ((index - cbuf->start) <= cbuf_size(cbuf)) + && ((cbuf->end - index) <= cbuf_size(cbuf)); +} + +static inline bool cbuf_range_valid(const struct cbuf *cbuf, size_t start, size_t end) { return (((end - start) <= cbuf_size(cbuf)) diff --git a/test/test_cbuf.c b/test/test_cbuf.c index ae660e3..53f967f 100644 --- a/test/test_cbuf.c +++ b/test/test_cbuf.c @@ -77,7 +77,7 @@ test_read_0(void) size = 0; error = cbuf_read(&cbuf, index, buf, &size); - check(error == EINVAL); + check(!error); check(size == 0); test_push(&cbuf, "a"); |