简体   繁体   中英

Flatbuffers causing Segmentation fault when sent over TCP with Boost ASIO

I am using FlatBuffers & Boost ASIO

Here is how I form up my message and sent it over from the client to the server:

size_t const header_length = 8;
size_t const body_size_b = 8;
std::string data;


ServerOpcode opc;
opc = ServerOpcode::SMSG_LOGIN_REQUEST_RESPONSE_TEST;

flatbuffers::FlatBufferBuilder builder;
auto name = builder.CreateString("Orc Monster");
auto accountRole = Vibranium::CreateAccountRole_Packet(builder,1,name);


std::ostringstream header_stream;
header_stream << std::setw(header_length) << std::hex << opc;

std::ostringstream body_size_stream;
body_size_stream << std::setw(body_size_b) << std::hex << builder.GetSize();

data += header_stream.str();
data += body_size_stream.str();
boost::asio::streambuf b;
std::ostream os(&b);

size_t request_length = data.length();

boost::asio::write(s, boost::asio::buffer(data,request_length));
boost::asio::write(s, boost::asio::buffer(&accountRole,builder.GetSize()));

Here is how I read it on server side:

void Vibranium::Client::read_header() {
    auto self(shared_from_this());
    _packet.header_.resize(Packet::header_size_in_bytes);
    boost::asio::async_read(socket,
    boost::asio::buffer(_packet.header_, Packet::header_size_in_bytes),
    [this, self](boost::system::error_code ec, std::size_t /*length*/)
    {
        if ((boost::asio::error::eof == ec) || (boost::asio::error::connection_reset == ec))
        {
            Disconnect();
        }
        else
        {
            std::ostringstream header_stream;
            header_stream << std::setw(Packet::header_size_in_bytes) << std::hex << _packet.header_;
            std::cout << "Header: " << std::endl;
            std::cout << std::hex << header_stream.str() << std::endl;
            read_body_size();
        }
    });
}
void Vibranium::Client::read_body_size() {
    auto self(shared_from_this());
    _packet.body_size_.resize(Packet::body_size_in_bytes);
    socket.async_read_some(boost::asio::buffer(_packet.body_size_, Packet::body_size_in_bytes),
   [this, self](boost::system::error_code ec, std::size_t length)
   {
       if ((boost::asio::error::eof == ec) || (boost::asio::error::connection_reset == ec))
       {
           Disconnect();
       }
       else
       {
           std::cout << "Body Size: " << std::endl;
           std::cout << _packet.body_size_ << std::endl;
           std::stringstream ss;
           ss << std::hex << _packet.body_size_;
           ss >> _packet.body_in_bytes;
           read_body();
       }
   });
}

void Vibranium::Client::read_body() {
    auto self(shared_from_this());
    _packet.body_.resize(_packet.body_in_bytes);
    socket.async_read_some(boost::asio::buffer(_packet.body_, _packet.body_in_bytes),
   [this, self](boost::system::error_code ec, std::size_t length)
   {
       if ((boost::asio::error::eof == ec) || (boost::asio::error::connection_reset == ec))
       {
           Disconnect();
       }
       else
       {
           try
           {
               auto ac = GetAccountRole_Packet(&_packet.body_);
               std::cout << ac->name()->str() << std::endl;
           }
           catch (std::exception& e)
           {
               std::cout << "Error bace!" << std::endl;
               // Unable to decode data.
               boost::system::error_code error(boost::asio::error::invalid_argument);
               return;
           }

           std::cout << "Body: " << std::endl;
           std::cout << _packet.body_ << std::endl;
           //Send(ServerOpcode::SMSG_AUTH_CONNECTION_RESPONSE,"How are you, mate?");
           read_header();
       }
   });
}


void Vibranium::Client::Disconnect() const {
    Logger::Log("Disconnected ID: " + std::__cxx11::to_string(this->connectionId), Logger::Error, true);
    for (int i = 0; i < Vibranium::Server::Clients.size(); ++i) {
        if(Vibranium::Server::Clients[i]->connectionId == this->connectionId)
            Vibranium::Server::Clients.erase(Vibranium::Server::Clients.begin() + i);
    }
}

When message is received on the server side I get the following output:

Header: 
     a99
Body Size: 
      24
Segmentation fault (core dumped)

Then if I try to see more details about the seg fault with gdb I see the following output:

Core was generated by `./AuthServer'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00007f0ad833c202 in flatbuffers::ReadScalar<int> (p=0x30bd6e0) at /usr/local/include/flatbuffers/base.h:392
392   return EndianScalar(*reinterpret_cast<const T *>(p));

Why is the server crashing? What am I doing wrong and how can I fix it?

PS I am pretty sure this line std::cout << ac->name()->str() << std::endl; is causing the crash, but I cant understand why.

Like I said in one of the other many other questions you opened on this topic, you first need to read the tutorial on FlatBuffers: https://google.github.io/flatbuffers/flatbuffers_guide_tutorial.html

Some hints:

  • You're not finishing the buffer.
  • You're not actually using the buffer anywhere in your writing code, only getting its size.
  • You seem to freely mixing binary and strings in your reading/writing. FlatBuffers need to be written as binary only. Stuff like std::hex is for text output.

Tip: just putting things together without understanding them may work in eg Python, but it leads to crashes in C++. You need to actually learn the language and APIs and how they work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM