chiark / gitweb /
buffers: Introduce buf_remaining_space
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 19 Sep 2014 23:03:20 +0000 (00:03 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 19 Sep 2014 23:07:22 +0000 (00:07 +0100)
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@chiark.greenend.org.uk>
slip.c
tun.c
udp.c
util.c
util.h

diff --git a/slip.c b/slip.c
index 17b3c18..17a9099 100644 (file)
--- 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) {
                }
                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"
                    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 (file)
--- 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());
     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()");
        }
        if (l<0) {
            fatal_perror("tun_afterpoll: read()");
        }
diff --git a/udp.c b/udp.c
index fa42ba4..552a58e 100644 (file)
--- 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,
            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;
                        0, (struct sockaddr *)&from, &fromlen);
            if (rv>0) {
                st->rbuf->size=rv;
diff --git a/util.c b/util.c
index 3bfa6bb..de182a4 100644 (file)
--- 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;
 
 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;
     p=buf->start + buf->size;
     buf->size+=amount;
     return p;
diff --git a/util.h b/util.h
index 1f43c5e..c2f10f8 100644 (file)
--- 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);
 
 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.
 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.