cppzmq icon indicating copy to clipboard operation
cppzmq copied to clipboard

std::move of zmq::message_t not behaving as expected

Open diehard2 opened this issue 1 year ago • 1 comments

I'm trying to pin a message_t in a way I can use it later for zero copy and I'm having a bear of a time doing it. In the below test the first one passes and the second one fails. I would have expected them both to pass since the underlying data isn't being memcpy'd anywhere. I can go with the first message, but its a bit unexpected to me that the first one fails.

TEST(ZMQTests, BUG)
{
  {
    zmq::multipart_t test;
    test.pushstr("echo2");

    zmq::multipart_t message;
    std::string_view view;

    zmq::message_t foo = test.pop();
    view = {static_cast<char*>(foo.data()), foo.size()};
    message = std::move(foo);

    EXPECT_EQ(std::string("echo2"), view);
  }
  {
    zmq::multipart_t test;
    test.pushstr("echo2");

    zmq::message_t message;
    std::string_view view;

    zmq::message_t foo = test.pop();
    view = {static_cast<char*>(foo.data()), foo.size()};
    message = std::move(foo);

    EXPECT_EQ(std::string("echo2"), view);
  }
}

diehard2 avatar Nov 18 '24 22:11 diehard2

You have a view into a moved from container, so I would say that is UB.

gummif avatar Nov 19 '24 09:11 gummif