serializeMsgPack to dinamically allocated buffer of size len falls short.
Created by: gnalbandian
Hi @bblanchon.
I am extensively using your library along with ESPASyncWebServer from @me-no-dev. Once a second I send to webSocket clients a stream of ~400bytes with data to update a web page data.
I am using a DynamicJsonBuffer to store all my data.
// data is serialized above this section
// buffer is defined above this section as well as len
// uint8_t* buffer = nullptr;
// size_t len = 0;
if(ws.count())
{
len = measureMsgPack(jsonDoc);
buffer = (uint8_t*)malloc(sizeof(uint8_t) * len);
if(buffer)
{
serializeMsgPack(jsonDoc, buffer, len); // **<- the issue relies here**
}
}
After successful buffer allocation and population, I send the data and free the buffer.
if(buffer)
{
ws.binaryAll(buffer, len);
free(buffer);
buffer = nullptr;
}
When reading the received data, I found out one value is missing. MessagePack is correctly decoded by javascript client, and it parses fine as a json. It's just the value of a key that instead of showing its current value, it shows always 0. It's data type is int32_t
After many test i found that increasing by 1 the length of the buffer size, I receive all the data as it should. ie:
serializeMsgPack(jsonDoc, buffer, len + 1); // **<- the issue relies here**
The following section has not been modified:
ws.binaryAll(buffer, len);
Never added +1
Unfortunately, applying this 'fix' leads to random crash, very likely because of buffer overflow, not sure thou. Can yo shed some light over here. Thanks @bblanchon