-
Notifications
You must be signed in to change notification settings - Fork 203
Open
Description
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
libsocket/C++/streamclient.cpp
Line 144 in 6214af7
| 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
Labels
No labels