commit 28be46ecbd5e9529ae0adddad60cf5c1e9718f88
parent 8621c464b1ddf50ffcd7cf8c65e915d2c74ddccc
Author: Stephen Gregoratto <dev@sgregoratto.me>
Date: Wed, 23 Sep 2020 21:02:17 +1000
fix an off-by-one error in readfile
size should always be filesize, the +1 for malloc should be...
for malloc. Doh!
Diffstat:
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/util.c b/util.c
@@ -215,7 +215,7 @@ offtout(off_t x, uint8_t *buf)
y = x < 0 ? -x : x;
- for (int i = 0; i <= 6; i++) {
+ for (size_t i = 0; i <= 6; i++) {
buf[i] = y % 256;
y -= buf[i];
y /= 256;
@@ -266,11 +266,11 @@ readfile(char *path, off_t *size)
* Allocate size + 1 bytes to ensure that we never
* try to malloc(0) and get a NULL pointer.
*/
- *size = st.st_size + 1;
- if ((buf = reallocarray(NULL, *size, sizeof(off_t))) == NULL)
+ *size = st.st_size;
+ if ((buf = reallocarray(NULL, *size + 1, sizeof(off_t))) == NULL)
err(1, "%s: reallocarray", path);
- if (read(fd, buf, *size) != *size)
+ if (read(fd, buf, *size) == -1)
err(1, "%s: read", path);
if (close(fd) == -1)
err(1, "%s: close", path);