From 9d0f14ed6aa75cf39b57c4e7efd5a1713e8ff27a Mon Sep 17 00:00:00 2001 From: Richard Braun Date: Mon, 7 Jan 2019 23:00:05 +0100 Subject: Revert "cbuf: remove range checking function" This reverts commit fc51b4a0b2f4f3e0546054b8262afded30e1f1c1. Turns out there are users of this function outside the library, so keep it around. --- src/cbuf.c | 4 ++-- src/cbuf.h | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/cbuf.c b/src/cbuf.c index e519578..372cc97 100644 --- a/src/cbuf.c +++ b/src/cbuf.c @@ -143,13 +143,13 @@ cbuf_write(struct cbuf *cbuf, size_t index, const void *buf, size_t size) uint8_t *start, *end, *buf_end; size_t new_end, skip; - if (!cbuf_index_valid(cbuf, index)) { + if (!cbuf_range_valid(cbuf, index, cbuf->end)) { return EINVAL; } new_end = index + size; - if (!cbuf_index_valid(cbuf, new_end)) { + if (!cbuf_range_valid(cbuf, cbuf->start, new_end)) { cbuf->end = new_end; cbuf_update_start(cbuf); diff --git a/src/cbuf.h b/src/cbuf.h index 9175bb4..437064e 100644 --- a/src/cbuf.h +++ b/src/cbuf.h @@ -89,6 +89,14 @@ cbuf_index_valid(const struct cbuf *cbuf, size_t index) && ((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)) + && ((start - cbuf->start) <= cbuf_size(cbuf)) + && ((cbuf->end - end) <= cbuf_size(cbuf))); +} + /* * Initialize a circular buffer. * -- cgit v1.2.3