-
Notifications
You must be signed in to change notification settings - Fork 46
rbuscli: fix coverity RESOURCE_LEAK in discovery commands #401
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
base: develop
Are you sure you want to change the base?
Conversation
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR fixes three Coverity RESOURCE_LEAK defects in the rbuscli tool by adding proper cleanup in error paths for discovery command functions. The changes add pointer initialization to NULL and cleanup code when RBUS discovery API calls fail, preventing potential memory leaks.
Key changes:
- Initialize pointer variables to NULL to prevent undefined behavior in error paths
- Add memory cleanup in error paths for three discovery functions
- Add explicit return statements after error handling to prevent fall-through
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
utils/rbuscli/rbuscli.c
Outdated
| if (destinations != NULL) | ||
| { | ||
| free(destinations); | ||
| } |
Copilot
AI
Nov 29, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The NULL check before free() is unnecessary and inconsistent with the rest of the codebase. In C, free(NULL) is well-defined and safe. The rest of this file calls free() directly without NULL checks (e.g., lines 953, 955, 991, 993, 1041, 1043, 1090, 1092).
Consider simplifying to:
free(destinations);| if (destinations != NULL) | |
| { | |
| free(destinations); | |
| } | |
| free(destinations); |
utils/rbuscli/rbuscli.c
Outdated
| if (pComponentNames != NULL) | ||
| { | ||
| free(pComponentNames); | ||
| } |
Copilot
AI
Nov 29, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The NULL check before free() is unnecessary and inconsistent with the rest of the codebase. In C, free(NULL) is well-defined and safe. The rest of this file calls free() directly without NULL checks (e.g., lines 953, 955, 991, 993, 1041, 1043, 1090, 1092).
Consider simplifying to:
free(pComponentNames);| if (pComponentNames != NULL) | |
| { | |
| free(pComponentNames); | |
| } | |
| free(pComponentNames); |
utils/rbuscli/rbuscli.c
Outdated
| if (pElementNames != NULL) | ||
| { | ||
| free(pElementNames); | ||
| } |
Copilot
AI
Nov 29, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The NULL check before free() is unnecessary and inconsistent with the rest of the codebase. In C, free(NULL) is well-defined and safe. The rest of this file calls free() directly without NULL checks (e.g., lines 953, 955, 991, 993, 1041, 1043, 1090, 1092).
Consider simplifying to:
free(pElementNames);| if (pElementNames != NULL) | |
| { | |
| free(pElementNames); | |
| } | |
| free(pElementNames); | |
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
Response to Copilot ReviewThank you @copilot-pull-request-reviewer for the excellent feedback! 🙏 Changes MadeI've addressed all 3 nitpick comments by removing the unnecessary NULL checks before You're absolutely right:
Changes: // Before (5 lines):
if (pComponentNames != NULL)
{
free(pComponentNames);
}
// After (1 line):
free(pComponentNames);Applied to all 3 functions:
Result:
The fix now matches the coding style of the rest of the file. Ready for re-review! |
|
@svc-rdkeportal01 I've opened a new pull request, #402, to work on those changes. Once the pull request is ready, I'll request review from you. |
Fix Coverity RESOURCE_LEAK in Discovery Commands
Coverity Issues Fixed
This PR fixes three related Coverity RESOURCE_LEAK defects in
utils/rbuscli/rbuscli.c:execute_discover_component_cmdexecute_discover_wildcard_dests_cmdexecute_discover_elements_cmdRoot Cause
All three functions had similar resource leak patterns:
rbus_discoverComponentName()was not freed in the error pathdestinationspointer + no cleanup whenrbus_discoverWildcardDestinations()failsrbus_discoverComponentDataElements()even whennumElements == 0, not freed in error pathChanges Made
For each function, the fix includes:
Initialize pointers to NULL to prevent undefined behavior
Add cleanup in error paths to prevent memory leaks
Add return statements to prevent fall-through to unintended code paths
Coverity Defect Details:
utils/rbuscli/rbuscli.c, functionexecute_discover_component_cmd()utils/rbuscli/rbuscli.c, functionexecute_discover_wildcard_dests_cmd()utils/rbuscli/rbuscli.c, functionexecute_discover_elements_cmd()Coverity Checker: RESOURCE_LEAK