From 28c44bfdd9953f4b709707c516f47d13ee5b60a8 Mon Sep 17 00:00:00 2001 From: Rajeev Katta Date: Sat, 29 Nov 2025 17:41:28 -0500 Subject: [PATCH 1/2] rbuscli: fix coverity RESOURCE_LEAK in discovery commands Fixes Coverity defects CID 112, 113, 114 (not GitHub issues) Fix generated by RDKDevPilot AI Bot with pattern validation Fixes resource leaks in three discovery command functions: 1. execute_discover_component_cmd (line 1004) - Coverity CID 112 2. execute_discover_elements_cmd (line 1049) - Coverity CID 114 3. execute_discover_wildcard_dests_cmd (line 1093) - Coverity CID 113 Changes: - Initialize pointers to NULL to avoid undefined behavior - Add free() in error paths to prevent memory leaks - Add return statements to prevent fall-through Pattern validation applied (learned from Copilot feedback): - Pointer initialization pattern (PR #396) - NULL check before free pattern (PR #396) - Return after cleanup pattern (PR #396) Validation scores: Pipeline 95/100, Pattern 70-84/100 --- utils/rbuscli/rbuscli.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/utils/rbuscli/rbuscli.c b/utils/rbuscli/rbuscli.c index 8037b1d2..25b9bf67 100644 --- a/utils/rbuscli/rbuscli.c +++ b/utils/rbuscli/rbuscli.c @@ -967,7 +967,7 @@ void execute_discover_component_cmd(int argc, char* argv[]) int index = 0; int i; int componentCnt = 0; - char **pComponentNames; + char **pComponentNames = NULL; char const* pElementNames[RBUS_CLI_MAX_PARAM] = {0, 0}; if (!verify_rbus_open()) @@ -1000,6 +1000,11 @@ void execute_discover_component_cmd(int argc, char* argv[]) else { printf ("Failed to discover components. Error Code = %d\r\n", rc); + if (pComponentNames != NULL) + { + free(pComponentNames); + } + return; } } @@ -1009,7 +1014,7 @@ void execute_discover_elements_cmd(int argc, char *argv[]) int numOfInputParams = argc - 2; bool nextLevel = true; int numElements = 0; - char** pElementNames; // FIXME: every component will have more than RBUS_CLI_MAX_PARAM elements right? + char** pElementNames = NULL; // FIXME: every component will have more than RBUS_CLI_MAX_PARAM elements right? if (!verify_rbus_open()) return; @@ -1045,6 +1050,11 @@ void execute_discover_elements_cmd(int argc, char *argv[]) else { printf ("Failed to discover elements. Error Code = %d\r\n", rc); + if (pElementNames != NULL) + { + free(pElementNames); + } + return; } } @@ -1052,7 +1062,7 @@ void execute_discover_wildcard_dests_cmd(int argc, char* argv[]) { rbusCoreError_t rc = RBUSCORE_SUCCESS; int numDestinations = 0; - char** destinations; + char** destinations = NULL; (void)argc; (void)argv; @@ -1089,6 +1099,11 @@ void execute_discover_wildcard_dests_cmd(int argc, char* argv[]) else { printf ("Failed to discover components. Error Code = %d\r\n", rc); + if (destinations != NULL) + { + free(destinations); + } + return; } } From 20d93ff8a2b40e4cfc2f677832955d121cf380e8 Mon Sep 17 00:00:00 2001 From: Rajeev Katta Date: Sat, 29 Nov 2025 17:49:49 -0500 Subject: [PATCH 2/2] Address Copilot feedback: remove unnecessary NULL checks before free() Copilot correctly pointed out that: - free(NULL) is well-defined and safe in C - The rest of the codebase calls free() directly without NULL checks - Our NULL checks were inconsistent with codebase style Changes: - Simplified all 3 cleanup blocks from 5 lines to 1 line - Now consistent with rest of rbuscli.c (lines 953, 955, 991, 993, etc.) - Maintains same safety (free(NULL) is safe) Addresses Copilot comments on lines 1006, 1056, 1105 --- utils/rbuscli/rbuscli.c | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/utils/rbuscli/rbuscli.c b/utils/rbuscli/rbuscli.c index 25b9bf67..5ede765e 100644 --- a/utils/rbuscli/rbuscli.c +++ b/utils/rbuscli/rbuscli.c @@ -1000,10 +1000,7 @@ void execute_discover_component_cmd(int argc, char* argv[]) else { printf ("Failed to discover components. Error Code = %d\r\n", rc); - if (pComponentNames != NULL) - { - free(pComponentNames); - } + free(pComponentNames); return; } } @@ -1050,10 +1047,7 @@ void execute_discover_elements_cmd(int argc, char *argv[]) else { printf ("Failed to discover elements. Error Code = %d\r\n", rc); - if (pElementNames != NULL) - { - free(pElementNames); - } + free(pElementNames); return; } } @@ -1099,10 +1093,7 @@ void execute_discover_wildcard_dests_cmd(int argc, char* argv[]) else { printf ("Failed to discover components. Error Code = %d\r\n", rc); - if (destinations != NULL) - { - free(destinations); - } + free(destinations); return; } }