commit ac86b2f9fece0e39be57b81e02cb9946a10df570
parent b64d3d5ac9121bd3c5df1c48defe5fdc209e467d
Author: Cédric Hannotier <cedric.hannotier@ulb.be>
Date: Thu, 29 Oct 2020 22:57:54 +0100
Separate path and read buffers & use snprintf
Diffstat:
1 file changed, 6 insertions(+), 5 deletions(-)
diff --git a/src/util.c b/src/util.c
@@ -68,15 +68,15 @@ int
download_resp(FILE *out, struct gemini_response resp, const char *path,
char *url)
{
- char buf[PATH_MAX];
+ char path_buf[PATH_MAX];
assert(path);
if (path[0] == '\0') {
path = "./";
}
if (path[strlen(path)-1] == '/') {
- strncat(strncpy(&buf[0], path, sizeof(buf)), basename(url),
- sizeof(buf));
- path = &buf[0];
+ int n = snprintf(path_buf, sizeof(path_buf), "%s%s", path, basename(url));
+ assert((size_t)n < sizeof(path_buf));
+ path = path_buf;
}
FILE *f = fopen(path, "w");
if (f == NULL) {
@@ -85,8 +85,9 @@ download_resp(FILE *out, struct gemini_response resp, const char *path,
return 1;
}
fprintf(out, "Downloading %s to %s\n", url, path);
+ char buf[BUFSIZ];
for (int n = 1; n > 0;) {
- n = BIO_read(resp.bio, buf, BUFSIZ);
+ n = BIO_read(resp.bio, buf, sizeof(buf));
if (n == -1) {
fprintf(stderr, "Error: read\n");
return 1;