Commit d7a0bf8e authored by Ryan Hansberry's avatar Ryan Hansberry Committed by Commit Bot

[Nearby] Correctly implement InputFile::Read().

The prior implementation of this function assumed that the provided
|size| would match the number of bytes read. In reality, |size| is
generally a very large value and number of bytes read may be smaller
than it.

Manually verified by successfully sending a 7KB file from Chrome OS
to Android.

Bug: 1124992
Change-Id: I781a2c1f69aee374a18c10641ed14f31eb7348f8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2393616Reviewed-by: default avatarJames Vecore <vecore@google.com>
Commit-Queue: Ryan Hansberry <hansberry@chromium.org>
Cr-Commit-Position: refs/heads/master@{#805409}
parent 3125314c
...@@ -30,12 +30,13 @@ ExceptionOr<ByteArray> InputFile::Read(std::int64_t size) { ...@@ -30,12 +30,13 @@ ExceptionOr<ByteArray> InputFile::Read(std::int64_t size) {
if (!file_.IsValid()) if (!file_.IsValid())
return Exception::kIo; return Exception::kIo;
ByteArray bytes(size); char buf[size];
int bytes_read = file_.ReadAtCurrentPos(bytes.data(), bytes.size()); int num_bytes_read = file_.ReadAtCurrentPos(buf, size);
if (bytes_read != size)
if (num_bytes_read < 0 || num_bytes_read > GetTotalSize())
return Exception::kIo; return Exception::kIo;
return ExceptionOr<ByteArray>(std::move(bytes)); return ExceptionOr<ByteArray>(ByteArray(buf, num_bytes_read));
} }
Exception InputFile::Close() { Exception InputFile::Close() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment