[PATCH 3/5] buffers: Introduce buf_remaining_space

Ian Jackson ijackson at chiark.greenend.org.uk
Sat Sep 20 00:43:18 BST 2014


This calculates the remaining space available to append to a buffer.

Use it in tun_afterpoll and udp_afterpoll (no functional change),
slip_unstuff and buf_append (fixes what appear to be latent bugs).

Signed-off-by: Ian Jackson <ijackson at chiark.greenend.org.uk>
---
 slip.c |    2 +-
 tun.c  |    2 +-
 udp.c  |    2 +-
 util.c |    2 +-
 util.h |    5 +++++
 5 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/slip.c b/slip.c
index 17b3c18..17a9099 100644
--- a/slip.c
+++ b/slip.c
@@ -127,7 +127,7 @@ static void slip_unstuff(struct slip *st, uint8_t *buf, uint32_t l)
 		}
 		st->buff->size=0;
 	    } else if (outputchr != OUTPUT_NOTHING) {
-		if (st->buff->size < st->buff->len) {
+		if (buf_remaining_space(st->buff)) {
 		    buf_append_uint8(st->buff,outputchr);
 		} else {
 		    Message(M_WARNING, "userv_afterpoll: dropping overlong"
diff --git a/tun.c b/tun.c
index c511dbe..d4070f2 100644
--- a/tun.c
+++ b/tun.c
@@ -116,7 +116,7 @@ static void tun_afterpoll(void *sst, struct pollfd *fds, int nfds)
     if (fds[0].revents&POLLIN) {
 	BUF_ALLOC(st->buff,"tun_afterpoll");
 	buffer_init(st->buff,calculate_max_start_pad());
-	l=read(st->fd,st->buff->start,st->buff->len-calculate_max_start_pad());
+	l=read(st->fd, st->buff->start, buf_remaining_space(st->buff));
 	if (l<0) {
 	    fatal_perror("tun_afterpoll: read()");
 	}
diff --git a/udp.c b/udp.c
index fa42ba4..552a58e 100644
--- a/udp.c
+++ b/udp.c
@@ -105,7 +105,7 @@ static void udp_afterpoll(void *state, struct pollfd *fds, int nfds)
 	    BUF_ALLOC(st->rbuf,"udp_afterpoll");
 	    buffer_init(st->rbuf,calculate_max_start_pad());
 	    rv=recvfrom(st->fd, st->rbuf->start,
-			(st->rbuf->base + st->rbuf->len) - st->rbuf->start,
+			buf_remaining_space(st->rbuf),
 			0, (struct sockaddr *)&from, &fromlen);
 	    if (rv>0) {
 		st->rbuf->size=rv;
diff --git a/util.c b/util.c
index 3bfa6bb..de182a4 100644
--- a/util.c
+++ b/util.c
@@ -254,7 +254,7 @@ void buffer_init(struct buffer_if *buffer, int32_t max_start_pad)
 
 void *buf_append(struct buffer_if *buf, int32_t amount) {
     void *p;
-    assert(buf->size <= buf->len - amount);
+    assert(amount <= buf_remaining_space(buf));
     p=buf->start + buf->size;
     buf->size+=amount;
     return p;
diff --git a/util.h b/util.h
index 1f43c5e..c2f10f8 100644
--- a/util.h
+++ b/util.h
@@ -29,6 +29,11 @@ extern void *buf_prepend(struct buffer_if *buf, int32_t amount);
 extern void *buf_unappend(struct buffer_if *buf, int32_t amount);
 extern void *buf_unprepend(struct buffer_if *buf, int32_t amount);
 
+static inline int32_t buf_remaining_space(const struct buffer_if *buf)
+{
+    return (buf->base + buf->len) - buf->start;
+}
+
 extern void buffer_readonly_view(struct buffer_if *n, const void*, int32_t len);
 extern void buffer_readonly_clone(struct buffer_if *n, const struct buffer_if*);
   /* Caller must only use unappend, unprepend et al. on n.
-- 
1.7.10.4




More information about the sgo-software-discuss mailing list