Repository navigation
fix: free cJSON strings printed for custom messages - #2257
Merged
Merged
Conversation
The custom message handler passed the result of cJSON_PrintUnformatted() straight to ESP_LOGI and to std::string, so both buffers leaked on every custom message, and a failed allocation built a std::string from NULL.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
With
CONFIG_RECEIVE_CUSTOM_MESSAGEenabled, thecustombranch inApplication::InitializeProtocol()callscJSON_PrintUnformatted()twice, once for theESP_LOGIand once to build thestd::stringcaptured by the scheduled lambda, and never frees either buffer. Every custom message from the server leaks two heap allocations the size of the JSON. If the allocation fails, the second call returns NULL andstd::string(NULL)aborts, since the firmware is built without C++ exceptions.The fix wraps both results in
CJsonStringUniquePtrfromcjson_utils.h, asmcp_server.halready does, logs an empty string if printing failed, and skips scheduling the chat message when the payload could not be printed.I checked it on a host by compiling the real
#if CONFIG_RECEIVE_CUSTOM_MESSAGEblock fromapplication.ccagainst cJSON 1.7.18 with-fno-exceptions, counting allocations throughcJSON_InitHooksand handling the same message 1000 times:The displayed message is unchanged on the normal path. I haven't flashed a board with this option enabled. #2040 carries a similar change inside a board support PR; this one only touches these lines so it can go in on its own.
AI tools used