Skip to content

Proposed solution to "double free or corruption (out)" #12

@chil-dk

Description

@chil-dk

I downloaded the code, and the examples didn't work.
I located what I think is an error in the buffer management.
The data type for the data waiting to be sent shouldn't be const char *, but strings.

struct Connection
{
    list<string>       buffer;     // <-- string and not const char*
    map<string,string> keyValueMap;
    time_t             createTime;
};

`
A copy of the incoming data should be made when pushing to the buffer:

A const char is just a pointer. If a pointer is pushed onto the buffer, and the pointer is for some reason no longer valid, then the buffer contains ... invalid data.
That's what I found when I put some debug statements into the code.
Here is what the echo server told me:

[cppWebSockets] Callback Receive
[cppWebSockets] Received: hjghjk
[cppWebSockets] send, socketID = 8 data = 'hjghjk'
[cppWebSockets] Callback Server Writeable, fd = 8
[cppWebSockets] == Elements in buffer: 1
[cppWebSockets] == Element : ���� [cppWebSockets] Sending '����'

The buffer elements was first pushed to the buffer in the WebSocketServer::send, and when the callback receive function asked for them, they were no longer valid.

Changing the datatype to string as shown solved the problem.

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