Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RDKBWIFI-17 Fixes for lack of cJSON_free for cJSON_Print output #117

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/cli/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ int main(int argc, const char *argv[])

if ((node = exec(line, strlen(line), NULL)) != NULL) {
if ((obj = (cJSON *)network_tree_to_json(node)) != NULL) {
printf("%s\n", cJSON_Print(obj));
char* tmp = cJSON_Print(obj);
printf("%s\n", tmp);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is cli which will be deprecated.

cJSON_free(tmp);
cJSON_Delete(obj);
}
free_network_tree(node);
Expand Down
1 change: 1 addition & 0 deletions src/cmd/em_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ char *em_cmd_t::status_to_string(em_cmd_out_status_t status, em_status_string_t

tmp = cJSON_Print(obj);
strncpy(str, tmp, strlen(tmp) + 1);
cJSON_free(tmp);
cJSON_Delete(obj);

return str;
Expand Down
13 changes: 11 additions & 2 deletions src/dm/dm_easy_mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,10 @@ int dm_easy_mesh_t::encode_config_reset(em_subdoc_info_t *subdoc, const char *ke

formatted_json = cJSON_Print(parent_obj);
printf("%s:%d: %s\n", __func__, __LINE__, formatted_json);
snprintf(subdoc->buff,sizeof(em_subdoc_data_buff_t),"%s",cJSON_Print(parent_obj));
cJSON_free(formatted_json);
char* tmp = cJSON_Print(parent_obj);
snprintf(subdoc->buff,sizeof(em_subdoc_data_buff_t),"%s",tmp);
cJSON_free(tmp);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not able to reproduce the issue again:
https://jira.rdkcentral.com/jira/browse/RDKBWIFI-17?focusedId=457298&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-457298

Engineer with higher experience in cJSON should give opinion if we can process this diff. I would like avoid double-free situation

Copy link
Contributor Author

@marcin-matula-2329518 marcin-matula-2329518 Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@soumyasmunshi let me know what do you think about that pull request. It seems evident that we have problem with memory leaks in unified-wifi-mesh due to cJSON_Print. I would like to avoid push this changes before your presentation for your bosses to avoid double-free-corruption (in the case if it is freed already).

All these memory leaks were detected by AddressSanitizer but I keep only one callstack: https://jira.rdkcentral.com/jira/browse/RDKBWIFI-17?focusedId=457298&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-457298

I think that this change can needed more tests (at least reproduce the issue). I will keep it as draft.

cJSON_Delete(parent_obj);
return 0;
}
Expand Down Expand Up @@ -608,7 +611,10 @@ int dm_easy_mesh_t::encode_config_test(em_subdoc_info_t *subdoc, const char *key

formatted_json = cJSON_Print(parent_obj);
printf("%s:%d: %s\n", __func__, __LINE__, formatted_json);
snprintf(subdoc->buff,sizeof(em_subdoc_data_buff_t),"%s",cJSON_Print(parent_obj));
cJSON_free(formatted_json);
char* tmp = cJSON_Print(parent_obj);
snprintf(subdoc->buff,sizeof(em_subdoc_data_buff_t),"%s",tmp);
cJSON_free(tmp);
cJSON_Delete(parent_obj);
return 0;
}
Expand Down Expand Up @@ -1888,6 +1894,7 @@ void dm_easy_mesh_t::create_autoconfig_renew_json_cmd(char* src_mac_addr, char*
cJSON_AddItemToObject(root, "wfa-dataelements:Renew", renew);
char* tmp = cJSON_Print(root);
snprintf(autoconfig_renew_json, sizeof(autoconfig_renew_json), "%s", tmp);
cJSON_free(tmp);
cJSON_Delete(root);
}

Expand All @@ -1909,6 +1916,7 @@ void dm_easy_mesh_t::create_ap_cap_query_json_cmd(char* src_mac_addr, char* agen
cJSON_AddItemToObject(root, "wfa-dataelements:Radiocap", query_info);
char* tmp = cJSON_Print(root);
snprintf(ap_query_json, sizeof(ap_query_json), "%s", tmp);
cJSON_free(tmp);
cJSON_Delete(root);
}

Expand All @@ -1931,6 +1939,7 @@ void dm_easy_mesh_t::create_client_cap_query_json_cmd(char* src_mac_addr, char*
cJSON_AddItemToObject(root, "wfa-dataelements:Clientcap", query_info);
char* tmp = cJSON_Print(root);
snprintf(ap_query_json, sizeof(ap_query_json), "%s", tmp);
cJSON_free(tmp);
cJSON_Delete(root);
}

Expand Down