From 316c2dec406fa5f07d31811147a21252342f726f Mon Sep 17 00:00:00 2001 From: Dr-Noob Date: Thu, 10 Feb 2022 22:27:51 +0100 Subject: [PATCH] [v1.01] Improve read_file implementation as suggested in #137 --- src/common/udev.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/common/udev.c b/src/common/udev.c index 395c637..2935e61 100644 --- a/src/common/udev.c +++ b/src/common/udev.c @@ -54,19 +54,18 @@ char* read_file(char* path, int* len) { //File exists, read it int bytes_read = 0; int offset = 0; - int block = 128; + int block = 1024; int buf_size = block * 4; char* buf = emalloc(sizeof(char) * buf_size); - memset(buf, 0, sizeof(char) * buf_size); while ((bytes_read = read(fd, buf+offset, block)) > 0) { offset += bytes_read; if(offset + block > buf_size) { - buf = erealloc(buf, sizeof(char) * buf_size * 2); - memset(buf + buf_size, 0, sizeof(char) * buf_size); - buf_size = buf_size * 2; + buf = erealloc(buf, sizeof(char) * (buf_size + block)); + buf_size += block; } } + buf[offset] = '\0'; if (close(fd) == -1) { return NULL;