commit b0ae028e1af0a7f6e3e828390109462cbd2ea6bb
parent 2ab1b868fbac189f3c7b8e2d4af14838aad1227f
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Mon, 12 Nov 2018 19:45:01 +0100
tls_read/read: buffer is not always filled in one read
+ change a MACRO to sizeof(buf).
Diffstat:
M | bget.c | | | 44 | +++++++++++++++++++++----------------------- |
1 file changed, 21 insertions(+), 23 deletions(-)
diff --git a/bget.c b/bget.c
@@ -184,7 +184,7 @@ https_request(void)
{
struct tls *t = NULL;
char buf[READ_BUF_SIZ], *p;
- size_t n, len = 0;
+ size_t n, len;
ssize_t r;
int fd = -1, httpok = 0, ret = 1;
@@ -221,16 +221,15 @@ https_request(void)
}
/* NOTE: HTTP header must fit in the buffer */
- r = tls_read(t, &buf, sizeof(buf));
- if (r == 0) {
- fprintf(stderr, "nothing read\n");
- goto err;
- }
- if (r == -1) {
- fprintf(stderr, "tls_read: %s\n", tls_error(t));
- goto err;
+ for (len = 0; len < sizeof(buf); len += r) {
+ if ((r = tls_read(t, &buf[len], sizeof(buf) - len)) == 0)
+ break;
+ if (r == -1) {
+ fprintf(stderr, "tls_read: %s\n", tls_error(t));
+ goto err;
+ }
}
- len += r;
+ buf[len] = '\0'; /* XXX: correct? */
if (!strncmp(buf, "HTTP/1.0 200 ", sizeof("HTTP/1.0 200 ") - 1) ||
!strncmp(buf, "HTTP/1.1 200 ", sizeof("HTTP/1.1 200 ") - 1))
@@ -244,7 +243,7 @@ https_request(void)
p += strlen("\r\n\r\n");
if (httpok) {
- n = r - (p - buf);
+ n = len - (p - buf);
r = fwrite(p, 1, n, stdout);
if (ferror(stdout)) {
fprintf(stderr, "fwrite: stdout: %s\n", strerror(errno));
@@ -297,7 +296,7 @@ int
http_request(void)
{
char buf[READ_BUF_SIZ], *p;
- size_t n, len = 0;
+ size_t n, len;
ssize_t r;
int fd = -1, httpok = 0, ret = 1;
@@ -322,16 +321,15 @@ http_request(void)
}
/* NOTE: HTTP header must fit in the buffer */
- r = read(fd, &buf, sizeof(buf));
- if (r == 0) {
- fprintf(stderr, "nothing read\n");
- goto err;
- }
- if (r == -1) {
- fprintf(stderr, "read: %s\n", strerror(errno));
- goto err;
+ for (len = 0; len < sizeof(buf); len += r) {
+ if ((r = read(fd, &buf[len], sizeof(buf) - len)) == 0)
+ break;
+ if (r == -1) {
+ fprintf(stderr, "read: %s\n", strerror(errno));
+ goto err;
+ }
}
- len += r;
+ buf[len] = '\0'; /* XXX: correct? */
if (!strncmp(buf, "HTTP/1.0 200 ", sizeof("HTTP/1.0 200 ") - 1) ||
!strncmp(buf, "HTTP/1.1 200 ", sizeof("HTTP/1.1 200 ") - 1))
@@ -345,7 +343,7 @@ http_request(void)
p += strlen("\r\n\r\n");
if (httpok) {
- n = r - (p - buf);
+ n = len - (p - buf);
r = fwrite(p, 1, n, stdout);
if (ferror(stdout)) {
fprintf(stderr, "fwrite: stdout: %s\n", strerror(errno));
@@ -415,7 +413,7 @@ gopher_request(void)
}
while (1) {
- r = read(fd, &buf, READ_BUF_SIZ);
+ r = read(fd, &buf, sizeof(buf));
if (r == 0)
break;
if (r == -1) {