Skip to content

Commit

Permalink
Ensure kk_read() appends to the buffer instead of overwriting.
Browse files Browse the repository at this point in the history
While there, reorganize it slightly to improve legibility, and reorganize kk_write() to match.
  • Loading branch information
dag-erling committed Sep 17, 2018
1 parent 86fd0f5 commit c52375f
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/kexkill.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,21 @@ kk_hup(struct kk_conn *conn)
static int
kk_read(struct kk_conn *conn)
{
unsigned char *buf;
ssize_t rlen;
size_t len;

if (conn->buflen == sizeof conn->buf) {
warnx("[%02x] buffer full", conn->fd);
return (-1);
}
buf = (unsigned char *)conn->buf + conn->buflen;
len = sizeof conn->buf - conn->buflen;
if ((rlen = read(conn->fd, conn->buf, len)) < 0) {
warn("[%02x] read()", conn->fd);
if (verbose)
warnx("[%02x] reading up to %zu bytes", conn->fd, len);
if ((rlen = read(conn->fd, buf, len)) < 0) {
if (verbose > 1)
warn("[%02x] read()", conn->fd);
return (-1);
}
conn->buflen += rlen;
Expand All @@ -178,18 +183,20 @@ kk_read(struct kk_conn *conn)
static int
kk_write(struct kk_conn *conn, const void *data, size_t len)
{
const unsigned char *buf;
ssize_t wlen;

if (verbose > 1)
warnx("[%02x] %s(%zu)", conn->fd, __func__, len);
buf = (const unsigned char *)data;
while (len > 0) {
if ((wlen = write(conn->fd, data, len)) < 0) {
if ((wlen = write(conn->fd, buf, len)) < 0) {
warn("[%02x] write()", conn->fd);
return (-1);
}
if (verbose > 1)
warnx("[%02x] wrote %zu bytes", conn->fd, len);
data = (const char *)data + wlen;
warnx("[%02x] wrote %zu bytes", conn->fd, wlen);
buf += wlen;
len -= wlen;
}
return (0);
Expand Down

0 comments on commit c52375f

Please sign in to comment.