From 59505329efed9e749b50398cefe4ff3fa9da50c9 Mon Sep 17 00:00:00 2001
From: przydatek <przydatek@google.com>
Date: Wed, 28 Aug 2019 04:47:25 -0700
Subject: [PATCH] Disallowing 0-count in FileRandomAccessStream::PRead(), to
 fit the RandomAccessStream interface.

PiperOrigin-RevId: 265883887
---
 cc/util/file_random_access_stream.cc      | 7 ++-----
 cc/util/file_random_access_stream_test.cc | 6 +++---
 2 files changed, 5 insertions(+), 8 deletions(-)

diff --git a/cc/util/file_random_access_stream.cc b/cc/util/file_random_access_stream.cc
index f0ddcc6c9..ca869bd6d 100644
--- a/cc/util/file_random_access_stream.cc
+++ b/cc/util/file_random_access_stream.cc
@@ -58,9 +58,9 @@ Status FileRandomAccessStream::PRead(int64_t position, int count,
     return ToStatusF(util::error::INVALID_ARGUMENT,
                      "dest_buffer must be non-null");
   }
-  if (count < 0) {
+  if (count <= 0) {
     return ToStatusF(util::error::INVALID_ARGUMENT,
-                     "count cannot be negative");
+                     "count must be positive");
   }
   if (count > dest_buffer->allocated_size()) {
     return ToStatusF(util::error::INVALID_ARGUMENT,
@@ -72,9 +72,6 @@ Status FileRandomAccessStream::PRead(int64_t position, int count,
   }
   crypto::tink::util::Status status = dest_buffer->set_size(count);
   if (!status.ok()) return status;
-  if (count == 0) {
-    return Status::OK;
-  }
   int read_count = pread(fd_, dest_buffer->get_mem_block(), count, position);
   if (read_count == 0) {
     dest_buffer->set_size(0).IgnoreError();
diff --git a/cc/util/file_random_access_stream_test.cc b/cc/util/file_random_access_stream_test.cc
index b0d1307f2..134d02db6 100644
--- a/cc/util/file_random_access_stream_test.cc
+++ b/cc/util/file_random_access_stream_test.cc
@@ -75,7 +75,7 @@ void ReadAndVerifyChunk(RandomAccessStream* ra_stream,
 }
 
 TEST(FileRandomAccessStreamTest, ReadingStreams) {
-  for (auto stream_size : {0, 10, 100, 1000, 10000, 1000000}) {
+  for (auto stream_size : {1, 10, 100, 1000, 10000, 1000000}) {
     SCOPED_TRACE(absl::StrCat("stream_size = ", stream_size));
     std::string file_contents;
     std::string filename = absl::StrCat(stream_size, "_reading_test.bin");
@@ -157,7 +157,7 @@ TEST(FileRandomAccessStreamTest, NegativeReadPosition) {
   }
 }
 
-TEST(FileRandomAccessStreamTest, NegativeReadCount) {
+TEST(FileRandomAccessStreamTest, NotPositiveReadCount) {
   for (auto stream_size : {0, 10, 100, 1000, 10000}) {
     std::string file_contents;
     std::string filename = absl::StrCat(stream_size, "_reading_test.bin");
@@ -166,7 +166,7 @@ TEST(FileRandomAccessStreamTest, NegativeReadCount) {
     auto ra_stream = absl::make_unique<util::FileRandomAccessStream>(input_fd);
     auto buffer = std::move(Buffer::New(42).ValueOrDie());
     int64_t position = 0;
-    for (auto count : {-100, -10, -1}) {
+    for (auto count : {-100, -10, -1, 0}) {
       SCOPED_TRACE(absl::StrCat("stream_size = ", stream_size,
                                 " count = ", count));
       auto status = ra_stream->PRead(position, count, buffer.get());
-- 
GitLab