From e7f1d41237f2504a0aeb8bcf62c8954b48f282c5 Mon Sep 17 00:00:00 2001 From: terryguan Date: Tue, 29 Oct 2024 13:20:46 -0700 Subject: [PATCH] read() can return fewer bytes than requested Sometimes read returns fewer bytes than requested. read() only read at most 0x7ffff000 bytes. Bug: 376247649 Test: manual, make mkbootfs, mkbootfs out/target/product../VENDOR_BOOT Change-Id: I8cbbae40c5f5c6c54d19bf77e9a801ed3390ed48 --- mkbootfs/mkbootfs.cpp | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/mkbootfs/mkbootfs.cpp b/mkbootfs/mkbootfs.cpp index 65cf497ac..a45c6a20a 100644 --- a/mkbootfs/mkbootfs.cpp +++ b/mkbootfs/mkbootfs.cpp @@ -19,6 +19,9 @@ #include #include +#include +#include + /* NOTES ** ** - see https://www.kernel.org/doc/Documentation/early-userspace/buffer-format.txt @@ -212,20 +215,12 @@ static void _archive(char *in, char *out, int ilen, int olen) if(lstat(in, &s)) err(1, "could not stat '%s'", in); if(S_ISREG(s.st_mode)){ - int fd = open(in, O_RDONLY); - if(fd < 0) err(1, "cannot open '%s' for read", in); - - char* tmp = (char*) malloc(s.st_size); - if(tmp == 0) errx(1, "cannot allocate %zd bytes", s.st_size); - - if(read(fd, tmp, s.st_size) != s.st_size) { - err(1, "cannot read %zd bytes", s.st_size); + std::string content; + if (!android::base::ReadFileToString(in, &content)) { + err(1, "cannot read '%s'", in); } - _eject(&s, out, olen, tmp, s.st_size); - - free(tmp); - close(fd); + _eject(&s, out, olen, content.data(), content.size()); } else if(S_ISDIR(s.st_mode)) { _eject(&s, out, olen, 0, 0); _archive_dir(in, out, ilen, olen);