[PATCH 1/3] scripts/common: Do not mmap in read_file_2()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Using mmap() in read_file_2() leads to problems because there are
cases where the buffer returned from read_file_2() is modified and
then written back to the same file. This doesn't work when the
original file has been mmapped instead of being read into an allocated
buffer.

Using mmap() was introduced for a usecase where the system is very tight
in memory and bareboximd ran out of memory. Support for this usecase is
removed here, we'll bring it back in the next patch.

Signed-off-by: Sascha Hauer <s.hauer@xxxxxxxxxxxxxx>
---
 scripts/common.c | 53 +++++++++++++++++++++++-------------------------
 1 file changed, 25 insertions(+), 28 deletions(-)

diff --git a/scripts/common.c b/scripts/common.c
index 154d6dffcb..3d07be3630 100644
--- a/scripts/common.c
+++ b/scripts/common.c
@@ -17,7 +17,7 @@
 int read_file_2(const char *filename, size_t *size, void **outbuf, size_t max_size)
 {
 	off_t fsize;
-	ssize_t rsize;
+	ssize_t read_size, now;
 	int ret, fd;
 	void *buf;
 
@@ -37,8 +37,10 @@ int read_file_2(const char *filename, size_t *size, void **outbuf, size_t max_si
 		goto close;
 	}
 
-	if (fsize < max_size)
-		max_size = fsize;
+	if (max_size < fsize)
+		read_size = max_size;
+	else
+		read_size = fsize;
 
 	if (lseek(fd, 0, SEEK_SET) == -1) {
 		fprintf(stderr, "Cannot seek to start %s: %s\n", filename, strerror(errno));
@@ -46,35 +48,30 @@ int read_file_2(const char *filename, size_t *size, void **outbuf, size_t max_si
 		goto close;
 	}
 
-	buf = mmap(NULL, max_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
-	if (buf == MAP_FAILED ) {
-		buf = malloc(max_size);
-		if (!buf) {
-			fprintf(stderr, "Cannot allocate memory\n");
-			ret = -ENOMEM;
-			goto close;
-		}
-
-		*outbuf = buf;
+	buf = malloc(read_size);
+	if (!buf) {
+		fprintf(stderr, "Cannot allocate memory\n");
+		ret = -ENOMEM;
+		goto close;
+	}
 
-		while (*size < max_size) {
-			rsize = read(fd, buf, max_size - *size);
-			if (rsize == 0) {
-				ret = -EIO;
-				goto free;
-			}
+	*outbuf = buf;
 
-			if (rsize < 0) {
-				ret = -errno;
-				goto free;
-			}
+	while (read_size) {
+		now = read(fd, buf, read_size);
+		if (now == 0) {
+			ret = -EIO;
+			goto free;
+		}
 
-			buf += rsize;
-			*size += rsize;
+		if (now < 0) {
+			ret = -errno;
+			goto free;
 		}
-	} else {
-		*outbuf = buf;
-		*size = max_size;
+
+		buf += now;
+		*size += now;
+		read_size -= now;
 	}
 
 	ret = 0;
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@xxxxxxxxxxxxxxxxxxx
http://lists.infradead.org/mailman/listinfo/barebox



[Index of Archives]     [Linux Embedded]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux