[mod_lua] Guard JSON::execute2() against NULL cJSON output - #3127
Open
fsliwenjie wants to merge 1 commit into
Open
[mod_lua] Guard JSON::execute2() against NULL cJSON output#3127fsliwenjie wants to merge 1 commit into
fsliwenjie wants to merge 1 commit into
Conversation
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.
mod_lua's JSON::execute2() crashes the whole FreeSWITCH process with SIGSEGV whenever the underlying switch_json_api_execute() call returns no reply.
Root cause
src/mod/languages/mod_lua/freeswitch_lua.cpp (JSON::execute2, both the const char * and SWIGLUA_TABLE overloads):
cpp cJSON *reply = execute(str); char *s = _return_unformatted_json ? cJSON_PrintUnformatted(reply) : cJSON_Print(reply); std::string result = std::string(s); /* <-- std::string(NULL) -> SIGSEGV */When the JSON API command fails / does not exist, switch_json_api_execute() leaves
eply == NULL. cJSON_Print(NULL) / cJSON_PrintUnformatted(NULL) then return NULL, and passing NULL to the std::string constructor is undefined behavior - on Linux it dereferences NULL and raises SIGSEGV, taking down every active call.
Fix
Null-check the pointer before constructing the std::string in both overloads:
cpp std::string result = std::string(s ? s : "");ree(NULL) and cJSON_Delete(NULL) are already safe no-ops, so the only unsafe spot was the std::string construction. A failed API call now yields an empty string instead of crashing the process.
Testing
Resolves: #3124