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
diff --git a/mkbootfs/mkbootfs.cpp b/mkbootfs/mkbootfs.cpp
index 65cf497..a45c6a2 100644
--- a/mkbootfs/mkbootfs.cpp
+++ b/mkbootfs/mkbootfs.cpp
@@ -19,6 +19,9 @@
#include <private/android_filesystem_config.h>
#include <private/fs_config.h>
+#include <android-base/file.h>
+#include <string>
+
/* NOTES
**
** - see https://www.kernel.org/doc/Documentation/early-userspace/buffer-format.txt
@@ -212,20 +215,12 @@
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);