serialize to char* with outputSize do not have terminator if the output is too small
pat1 opened this issue · 1 comments
pat1 commented
Describe the bug
calling:
size_t serializeJson(const JsonDocument& doc, char* output, size_t outputSize);
if output is too small it do not have the terminator
Troubleshooter report
Here is the report generated by the ArduinoJson Troubleshooter:
- The issue happens at run time
- The issue concerns serialization
- Output is incomplete
- Program doesn't call
serializeJson()
in a loop JsonDocument::overflowed()
returnsfalse
- No string contains a NUL
Environment
Here is the environment that I used:
- Microcontroller: avr atmega1284p
- Core/runtime: platformio
Reproduction
to check the ploblem I have to do something like this:
if (serializeJson(json,json_buffer, json_buffer_length) == json_buffer_length){
json_buffer[0]='\0';
}
bblanchon commented
Hi @pat1,
I'm not sure we can qualify that as a bug.
Sure, it's error-prone, just like strncpy()
, but changing the behavior would break this legitimate use case:
size_t n = measureJson(doc);
char* json = malloc(n);
serializeJson(doc, json, n);
send(json, n);
free(n);
or, as I often see in Arduino code:
size_t n = measureJson(doc);
char json[n];
serializeJson(doc, json, n);
send(json, n);
However, I think this caveat should be documented, so I added a warning in the documentation.
Best regards,
Benoit