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>
}
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"
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()");
}
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;
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;
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.