Skip to content

Commit

Permalink
fix buffer-related output corruption of vty_out().
Browse files Browse the repository at this point in the history
Signed-off-by: Yasuhiro Ohara <[email protected]>
  • Loading branch information
Yasuhiro Ohara committed Sep 28, 2023
1 parent 0e2c914 commit ffe483f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
25 changes: 25 additions & 0 deletions lib/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,31 @@ void buffer_put(struct buffer *b, const void *p, size_t size)
}
}

/* buffer_put() and terminate the buffer with '\0' */
void buffer_put_strnul(struct buffer *b, const void *p, size_t size)
{
struct buffer_data *data = b->tail;
const char *ptr = p;

/* We use even last one byte of data buffer. */
while (size) {
size_t chunk;

/* If there is no data buffer add it. */
if (data == NULL || data->cp + 1 == b->size)
data = buffer_add(b);

chunk = ((size <= (b->size - data->cp - 1)) ?
size : (b->size - data->cp - 1));
memcpy((data->data + data->cp), ptr, chunk);
size -= chunk;
ptr += chunk;
data->cp += chunk;
data->data[data->cp] = '\0';
}
}


/* Insert character into the buffer. */
void buffer_putc(struct buffer *b, uint8_t c)
{
Expand Down
2 changes: 2 additions & 0 deletions lib/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ extern void buffer_free(struct buffer *);

/* Add the given data to the end of the buffer. */
extern void buffer_put(struct buffer *, const void *, size_t);
/* Add the given data to the end of the buffer, terminate with NUL. */
extern void buffer_put_strnul(struct buffer *, const void *, size_t);
/* Add a single character to the end of the buffer. */
extern void buffer_putc(struct buffer *, uint8_t);
/* Add a NUL-terminated string to the end of the buffer. */
Expand Down
2 changes: 1 addition & 1 deletion lib/vty.c
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ int vty_out(struct vty *vty, const char *format, ...)
nlines--;
vty->vtysh_lines += nlines;
}
buffer_put(vty->obuf, (uint8_t *)filtered, strlen(filtered));
buffer_put_strnul(vty->obuf, (uint8_t *)filtered, strlen(filtered));
break;
case VTY_SHELL_SERV:
case VTY_FILE:
Expand Down

0 comments on commit ffe483f

Please sign in to comment.