arrow-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From u..@apache.org
Subject arrow git commit: ARROW-1344: [C++] Do not permit writing to closed BufferOutputStream
Date Tue, 29 Aug 2017 08:18:18 GMT
Repository: arrow
Updated Branches:
  refs/heads/master 0ced74e1e -> e6456b6b3


ARROW-1344: [C++] Do not permit writing to closed BufferOutputStream

Since this API code path in Python, it would be better to not segfault when writing to a BufferOutputStream
whose internal buffer has been closed.

Author: Wes McKinney <wes.mckinney@twosigma.com>

Closes #1004 from wesm/ARROW-1344 and squashes the following commits:

2ee0454 [Wes McKinney] Do not shrink to fit in BufferOutputStream
02c7c28 [Wes McKinney] Do not permit writing to closed BufferOutputStream


Project: http://git-wip-us.apache.org/repos/asf/arrow/repo
Commit: http://git-wip-us.apache.org/repos/asf/arrow/commit/e6456b6b
Tree: http://git-wip-us.apache.org/repos/asf/arrow/tree/e6456b6b
Diff: http://git-wip-us.apache.org/repos/asf/arrow/diff/e6456b6b

Branch: refs/heads/master
Commit: e6456b6b35d03a647cb69b15c043efeded29318d
Parents: 0ced74e
Author: Wes McKinney <wes.mckinney@twosigma.com>
Authored: Tue Aug 29 10:18:13 2017 +0200
Committer: Uwe L. Korn <uwelk@xhochy.com>
Committed: Tue Aug 29 10:18:13 2017 +0200

----------------------------------------------------------------------
 cpp/src/arrow/io/io-memory-test.cc | 12 ++++++++++++
 cpp/src/arrow/io/memory.cc         |  7 ++++++-
 cpp/src/arrow/io/memory.h          |  1 +
 3 files changed, 19 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/arrow/blob/e6456b6b/cpp/src/arrow/io/io-memory-test.cc
----------------------------------------------------------------------
diff --git a/cpp/src/arrow/io/io-memory-test.cc b/cpp/src/arrow/io/io-memory-test.cc
index 75365df..0d45c63 100644
--- a/cpp/src/arrow/io/io-memory-test.cc
+++ b/cpp/src/arrow/io/io-memory-test.cc
@@ -67,6 +67,18 @@ TEST_F(TestBufferOutputStream, CloseResizes) {
   ASSERT_EQ(static_cast<int64_t>(K * data.size()), buffer_->size());
 }
 
+TEST_F(TestBufferOutputStream, WriteAfterFinish) {
+  std::string data = "data123456";
+  ASSERT_OK(stream_->Write(data));
+
+  auto buffer_stream = static_cast<BufferOutputStream*>(stream_.get());
+
+  std::shared_ptr<Buffer> buffer;
+  ASSERT_OK(buffer_stream->Finish(&buffer));
+
+  ASSERT_RAISES(IOError, stream_->Write(data));
+}
+
 TEST(TestFixedSizeBufferWriter, Basics) {
   std::shared_ptr<MutableBuffer> buffer;
   ASSERT_OK(AllocateBuffer(default_memory_pool(), 1024, &buffer));

http://git-wip-us.apache.org/repos/asf/arrow/blob/e6456b6b/cpp/src/arrow/io/memory.cc
----------------------------------------------------------------------
diff --git a/cpp/src/arrow/io/memory.cc b/cpp/src/arrow/io/memory.cc
index 532c2c6..568d7b2 100644
--- a/cpp/src/arrow/io/memory.cc
+++ b/cpp/src/arrow/io/memory.cc
@@ -41,6 +41,7 @@ static constexpr int64_t kBufferMinimumSize = 256;
 
 BufferOutputStream::BufferOutputStream(const std::shared_ptr<ResizableBuffer>&
buffer)
     : buffer_(buffer),
+      is_open_(true),
       capacity_(buffer->size()),
       position_(0),
       mutable_data_(buffer->mutable_data()) {}
@@ -62,7 +63,7 @@ BufferOutputStream::~BufferOutputStream() {
 
 Status BufferOutputStream::Close() {
   if (position_ < capacity_) {
-    return buffer_->Resize(position_);
+    return buffer_->Resize(position_, false);
   } else {
     return Status::OK();
   }
@@ -72,6 +73,7 @@ Status BufferOutputStream::Finish(std::shared_ptr<Buffer>* result)
{
   RETURN_NOT_OK(Close());
   *result = buffer_;
   buffer_ = nullptr;
+  is_open_ = false;
   return Status::OK();
 }
 
@@ -81,6 +83,9 @@ Status BufferOutputStream::Tell(int64_t* position) const {
 }
 
 Status BufferOutputStream::Write(const uint8_t* data, int64_t nbytes) {
+  if (ARROW_PREDICT_FALSE(!is_open_)) {
+    return Status::IOError("OutputStream is closed");
+  }
   DCHECK(buffer_);
   RETURN_NOT_OK(Reserve(nbytes));
   memcpy(mutable_data_ + position_, data, nbytes);

http://git-wip-us.apache.org/repos/asf/arrow/blob/e6456b6b/cpp/src/arrow/io/memory.h
----------------------------------------------------------------------
diff --git a/cpp/src/arrow/io/memory.h b/cpp/src/arrow/io/memory.h
index 667a7f0..75bc3d0 100644
--- a/cpp/src/arrow/io/memory.h
+++ b/cpp/src/arrow/io/memory.h
@@ -62,6 +62,7 @@ class ARROW_EXPORT BufferOutputStream : public OutputStream {
   Status Reserve(int64_t nbytes);
 
   std::shared_ptr<ResizableBuffer> buffer_;
+  bool is_open_;
   int64_t capacity_;
   int64_t position_;
   uint8_t* mutable_data_;


Mime
View raw message