commit e5bf82213147abc9abbac758611fd7ed52d71bf7
parent b812bd1c77fa47277f75c5d161962ce4941176c3
Author: Jacob R. Edwards <n/a>
Date: Thu, 13 Oct 2022 18:21:32 -0700
Fix previously introduced bufgetline function
The issue was that the line length used was 1 byte too short.
In addition to the fix, it was modified to return 0 when it gets a
line; the value of the line pointer must be used to determine whether
0 means a line was gotten or not. Usage of the never seen version
which returned a ssize_t line length was also updated.
Diffstat:
3 files changed, 5 insertions(+), 6 deletions(-)
diff --git a/aps/aps.c b/aps/aps.c
@@ -237,7 +237,7 @@ aps_command(struct aps *aps, int fd)
unsigned int i;
buf = NULL;
- if (aps_read(aps, fd, &buf) < 0)
+ if (aps_read(aps, fd, &buf))
return -1;
if (buf == NULL)
return 0;
diff --git a/lib/ap/buf.c b/lib/ap/buf.c
@@ -151,12 +151,12 @@ bufgetline(struct buf *buf, char **line)
if (!(end = memchr(buf->data, '\n', buf->len)))
return 0;
- len = end - buf->data;
+ len = end - buf->data + 1;
if (!(*line = malloc(len)))
return -1;
memcpy(*line, buf->data, len - 1);
- (*line)[len] = 0;
+ (*line)[len - 1] = 0;
bufshift(buf, len);
return 1;
}
diff --git a/lib/ap/client.c b/lib/ap/client.c
@@ -81,7 +81,6 @@ apc_write(struct apc *apc)
char *
apc_read(struct apc *apc)
{
- ssize_t len;
char *buf;
if (apc->status) {
@@ -90,10 +89,10 @@ apc_read(struct apc *apc)
}
buf = NULL;
- while ((len = bufgetline(apc->response, &buf)) == 0)
+ while (!bufgetline(apc->response, &buf) && !buf)
if (bufread(apc->response, apc->con->sock, 4096) <= 0)
return NULL;
- if (len < 0)
+ if (!buf)
return NULL;
if (strcmp(buf, "ok") == 0 || strncmp(buf, "error: ", 7) == 0) {