Skip to content

stream_client_socket::operator>> forms invalid std::string on write #91

@YarikMamykin

Description

@YarikMamykin

The operator>> of socket forms incorrect std::string.
If I try to read message from socket with >> I get "nothing", because std::string was not resized properly. See the code snippet below:

    // Assume that client sends "test message" after connection
    
    libsocket::inet_stream_server server("127.0.0.1", "8080", LIBSOCKET_IPv4);

    auto client = server.accept2();
   
    std::string received_msg;
    *client >> received_msg;

   // Next line prints `[SERVER] Received: `
   std::cout << "[SERVER] Received: " << received_msg << std::endl;   

   // Next line prints `[SERVER] Received: test message`
   std::cout << "[SERVER] Received: " << received_msg.data() << std::endl;

As I understood problem hides in

if (read_bytes < static_cast<ssize_t>(dest.size()))

The condition read_bytes < static_cast<ssize_t>(dest.size()) doesn't work for case, when dest is a newly created std::string and read_bytes is greater than 0.

I suggest to remove if statement and always resize dest to read_bytes size or just invert the condition.

I will create the PR for this issue later.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions