commit 3881495ad1755faadda166e250e44967e2a25f95
parent 52bb19476ac4dae399ad5029215d8dcb61dc77a0
Author: Jacob R. Edwards <n/a>
Date: Sun, 9 Oct 2022 09:23:32 -0700
Introduce a standard interface to shifting buffer contents
Diffstat:
4 files changed, 16 insertions(+), 6 deletions(-)
diff --git a/aps/aps.c b/aps/aps.c
@@ -160,8 +160,7 @@ aps_respond(struct aps *aps, int fd)
buf->len = 0;
aps->pfds[fd].events = POLLIN;
} else {
- memmove(buf->data, buf->data + n, buf->len - n);
- buf->len -= n;
+ bufshift(buf, n);
}
return 0;
}
@@ -280,8 +279,7 @@ aps_command(struct aps *aps, int s)
exit:
len = next - (char *)buf->data;
- memmove(buf->data, buf->data + len, buf->len - len);
- buf->len -= len;
+ bufshift(buf, len);
free(argv);
return aps_bufstatus(aps, s, status);
diff --git a/lib/ap/buf.c b/lib/ap/buf.c
@@ -93,6 +93,18 @@ bufappend(struct buf *buf, void *data, size_t len)
}
int
+bufshift(struct buf *buf, size_t n)
+{
+ if (n > buf->len) {
+ errno = EOVERFLOW;
+ return 1;
+ }
+ memmove(buf->data, buf->data + n, buf->len - n);
+ buf->len -= n;
+ return 0;
+}
+
+int
bufword(struct buf *buf, char *word)
{
size_t len;
diff --git a/lib/ap/buf.h b/lib/ap/buf.h
@@ -9,5 +9,6 @@ void buffree(struct buf *);
int bufresize(struct buf *, size_t);
int bufenlarge(struct buf *, size_t);
int bufappend(struct buf *, void *, size_t);
+int bufshift(struct buf *, size_t);
int bufword(struct buf *, char *);
int bufline(struct buf *, char *);
diff --git a/lib/ap/client.c b/lib/ap/client.c
@@ -115,8 +115,7 @@ apc_read(struct apc *apc)
memcpy(buf, apc->response->data, len);
buf[len] = 0;
- memmove(apc->response->data, end + 1,
- apc->response->len -= len + 1);
+ bufshift(apc->response, len + 1);
if (strcmp(buf, "ok") == 0 || strncmp(buf, "error: ", 7) == 0) {
if (*buf == 'e') {