felipecrv commented on code in PR #39164:
URL: https://github.com/apache/arrow/pull/39164#discussion_r1428357654


##########
cpp/src/arrow/ipc/message.cc:
##########
@@ -626,10 +626,24 @@ class MessageDecoder::MessageDecoderImpl {
             RETURN_NOT_OK(ConsumeMetadataLengthData(data, 
next_required_size_));
             break;
           case State::METADATA: {
-            auto buffer = std::make_shared<Buffer>(data, next_required_size_);
+            // We need to copy metadata because it's used in
+            // ConsumeBody(). ConsumeBody() may be called from another
+            // ConsumeData(). We can't assume that the given data for
+            // the current ConsumeData() call is still valid in the
+            // next ConsumeData() call. So we need to copy metadata
+            // here.
+            ARROW_ASSIGN_OR_RAISE(std::shared_ptr<Buffer> buffer,
+                                  AllocateBuffer(next_required_size_, pool_));
+            memcpy(buffer->mutable_data(), data, next_required_size_);

Review Comment:
   Wouldn't this be solved more elegantly by changing `ConsumeMetadataBuffer`?
   
   ```diff
      Status ConsumeMetadataBuffer(const std::shared_ptr<Buffer>& buffer) {
   -    if (buffer->is_cpu()) {
   -      metadata_ = buffer;
   -    } else {
          ARROW_ASSIGN_OR_RAISE(metadata_,
                                Buffer::ViewOrCopy(buffer, 
CPUDevice::memory_manager(pool_)));
   -    }
        return ConsumeMetadata();
      }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to