aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOmar Polo <op@omarpolo.com>2023-06-11 09:21:34 +0000
committerOmar Polo <op@omarpolo.com>2023-06-11 09:21:34 +0000
commit4ad573d0d5675212b0b5719a0a5c1de22974dd0e (patch)
tree2742a28662619d3e0b89b47629f86982ed54e105
parent1a99859b357957715fb62ced6ddef871ca9ab3a0 (diff)
rework load_file to use pread()
avoids issues since the same file is sent to multiple processes after being dup()'ed. Since these files are meant to be regular files, I don't expect short reads.
-rw-r--r--config.c18
1 files changed, 7 insertions, 11 deletions
diff --git a/config.c b/config.c
index f22f44c..de266c4 100644
--- a/config.c
+++ b/config.c
@@ -419,18 +419,14 @@ static int
load_file(int fd, uint8_t **data, size_t *len)
{
struct stat sb;
- FILE *fp;
- size_t r;
+ ssize_t r;
if (fstat(fd, &sb) == -1)
fatal("fstat");
- if ((fp = fdopen(fd, "r")) == NULL)
- fatal("fdopen");
-
if (sb.st_size < 0 /* || sb.st_size > SIZE_MAX */) {
log_warnx("file too large");
- fclose(fp);
+ close(fd);
return -1;
}
*len = sb.st_size;
@@ -438,15 +434,15 @@ load_file(int fd, uint8_t **data, size_t *len)
if ((*data = malloc(*len)) == NULL)
fatal("malloc");
- r = fread(*data, 1, *len, fp);
- if (r != *len) {
- log_warn("read");
- fclose(fp);
+ r = pread(fd, *data, *len, 0);
+ if (r == -1 || (size_t)r != *len) {
+ log_warn("read failed");
+ close(fd);
free(*data);
return -1;
}
- fclose(fp);
+ close(fd);
return 0;
}