ReadFully: don't use stale errno on EOF
Since EOF is not an error condition for read(2), we weren't setting
errno despite returning false. This wasn't explicitly wrong, because
we didn't mention errno in the docs for ReadFully, but some clients
were logging it.
There's no good errno value for "short read" - we picked ENODATA as
the least worst option.
Bug: 302723053
Test: mma
Test: binderRpcTest on Linux Binder
Change-Id: I439a3a49a96bcb49baff0799fbfc4ac0d09ca66e
diff --git a/file.cpp b/file.cpp
index e433a07..f71773b 100644
--- a/file.cpp
+++ b/file.cpp
@@ -313,7 +313,11 @@
size_t remaining = byte_count;
while (remaining > 0) {
ssize_t n = TEMP_FAILURE_RETRY(read(fd.get(), p, remaining));
- if (n <= 0) return false;
+ if (n == 0) { // EOF
+ errno = ENODATA;
+ return false;
+ }
+ if (n == -1) return false;
p += n;
remaining -= n;
}
@@ -358,7 +362,11 @@
uint8_t* p = reinterpret_cast<uint8_t*>(data);
while (byte_count > 0) {
ssize_t n = TEMP_FAILURE_RETRY(pread(fd.get(), p, byte_count, offset));
- if (n <= 0) return false;
+ if (n == 0) { // EOF
+ errno = ENODATA;
+ return false;
+ }
+ if (n == -1) return false;
p += n;
byte_count -= n;
offset += n;