ART: ReadFully needs to fail when EOF
Besides an explicit error, we should stop trying when we unexpectedly
reach the end of the file.
Change-Id: I16d601e1b5bcbc39cb8c4dd7fd3dbb7d69016579
diff --git a/runtime/base/unix_file/fd_file.cc b/runtime/base/unix_file/fd_file.cc
index 6d5b59c..f29a7ec 100644
--- a/runtime/base/unix_file/fd_file.cc
+++ b/runtime/base/unix_file/fd_file.cc
@@ -122,7 +122,9 @@
char* ptr = static_cast<char*>(buffer);
while (byte_count > 0) {
ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd_, ptr, byte_count));
- if (bytes_read == -1) {
+ if (bytes_read <= 0) {
+ // 0: end of file
+ // -1: error
return false;
}
byte_count -= bytes_read; // Reduce the number of remaining bytes.
diff --git a/runtime/base/unix_file/fd_file_test.cc b/runtime/base/unix_file/fd_file_test.cc
index d620666..33b3d3e 100644
--- a/runtime/base/unix_file/fd_file_test.cc
+++ b/runtime/base/unix_file/fd_file_test.cc
@@ -16,6 +16,7 @@
#include "base/unix_file/fd_file.h"
#include "base/unix_file/random_access_file_test.h"
+#include "common_runtime_test.h" // For ScratchFile
#include "gtest/gtest.h"
namespace unix_file {
@@ -60,4 +61,15 @@
EXPECT_TRUE(file.IsOpened());
}
+TEST_F(FdFileTest, ReadFullyEmptyFile) {
+ // New scratch file, zero-length.
+ art::ScratchFile tmp;
+ FdFile file;
+ ASSERT_TRUE(file.Open(tmp.GetFilename(), O_RDONLY));
+ EXPECT_GE(file.Fd(), 0);
+ EXPECT_TRUE(file.IsOpened());
+ uint8_t buffer[16];
+ EXPECT_FALSE(file.ReadFully(&buffer, 4));
+}
+
} // namespace unix_file