Skip to content

Commit

Permalink
http: fix read_file on win32 (incorrect get fsize)
Browse files Browse the repository at this point in the history
  • Loading branch information
fAuernigg committed Feb 15, 2023
1 parent 0d276f5 commit 811e5de
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions src/http/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#define DEBUG_LEVEL 5
#include <re_dbg.h>

#define PEMBUF_SIZE 512

enum {
CONN_TIMEOUT = 30000,
Expand Down Expand Up @@ -728,9 +729,8 @@ static void query_handler(int err, const struct dnshdr *hdr, struct list *ansl,
static int read_file(char **pbuf, const char *path)
{
FILE *f = NULL;
long s = 0;
size_t n = 0;
char *buf;
size_t n = 0, idx = 0;
char *buf, *nbuf;

if (!pbuf || !path)
return EINVAL;
Expand All @@ -741,28 +741,28 @@ static int read_file(char **pbuf, const char *path)
return EIO;
}

fseek(f, 0L, SEEK_END);
s = ftell(f);
if (s < 0) {
fclose(f);
return errno;
}
fseek(f, 0L, SEEK_SET);

buf = mem_zalloc(s + 1, NULL);
buf = mem_zalloc(PEMBUF_SIZE, NULL);
if (!buf) {
DEBUG_WARNING("Could not allocate cert file buffer\n");
fclose(f);
return ENOMEM;
}

n = fread(buf, 1, s, f);
fclose(f);
if (n < (size_t)s) {
mem_deref(buf);
return EIO;
while ((n=fread(buf+idx, 1, PEMBUF_SIZE, f)) == PEMBUF_SIZE) {
idx += n;
nbuf = mem_realloc(buf, idx + PEMBUF_SIZE);
if (!nbuf) {
DEBUG_WARNING("Out of memory while reading cert file"\
"'%s',\n", path);
mem_deref(buf);
return ENOMEM;
}
buf = nbuf;
memset(buf+idx, 0, PEMBUF_SIZE);
}

fclose(f);

*pbuf = buf;
return 0;
}
Expand Down

0 comments on commit 811e5de

Please sign in to comment.