summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Braun <rbraun@sceen.net>2017-06-22 20:44:15 +0200
committerRichard Braun <rbraun@sceen.net>2017-06-22 20:44:15 +0200
commitbe5eb1da462a2d715172a98aa236d4d88dbd557c (patch)
tree2e6ed6b52ae84fe9ca89abafc7b1dcf716ca1e34
parent4ec81c66c2339d31ae5dc3d628753e0d216862d8 (diff)
kern/cbuf: new cbuf_range_valid function
-rw-r--r--kern/cbuf.c10
-rw-r--r--kern/cbuf.h7
2 files changed, 12 insertions, 5 deletions
diff --git a/kern/cbuf.c b/kern/cbuf.c
index cd8ee87d..f00532ef 100644
--- a/kern/cbuf.c
+++ b/kern/cbuf.c
@@ -78,13 +78,13 @@ cbuf_write(struct cbuf *cbuf, size_t index, const void *buf, size_t size)
char *start, *end, *buf_end;
size_t new_end, skip;
- if ((cbuf->end - index) > cbuf_size(cbuf)) {
+ if (!cbuf_range_valid(cbuf, index, cbuf->end)) {
return ERROR_INVAL;
}
new_end = index + size;
- if ((new_end - cbuf->start) > cbuf_size(cbuf)) {
+ if (!cbuf_range_valid(cbuf, cbuf->start, new_end)) {
cbuf->end = new_end;
if (size > cbuf_capacity(cbuf)) {
@@ -118,13 +118,13 @@ cbuf_read(const struct cbuf *cbuf, size_t index, void *buf, size_t *sizep)
const char *start, *end, *buf_end;
size_t size;
- size = cbuf->end - index;
-
/* At least one byte must be available */
- if ((size - 1) >= cbuf_size(cbuf)) {
+ if (!cbuf_range_valid(cbuf, index, index + 1)) {
return ERROR_INVAL;
}
+ size = cbuf->end - index;
+
if (*sizep > size) {
*sizep = size;
}
diff --git a/kern/cbuf.h b/kern/cbuf.h
index 0558cc09..a30e5f28 100644
--- a/kern/cbuf.h
+++ b/kern/cbuf.h
@@ -21,6 +21,7 @@
#ifndef _KERN_CBUF_H
#define _KERN_CBUF_H
+#include <stdbool.h>
#include <stddef.h>
/*
@@ -66,6 +67,12 @@ cbuf_clear(struct cbuf *cbuf)
cbuf->start = cbuf->end;
}
+static inline bool
+cbuf_range_valid(const struct cbuf *cbuf, size_t start, size_t end)
+{
+ return ((end - start) <= cbuf_size(cbuf));
+}
+
/*
* Initialize a circular buffer.
*