RDKCOM-5655: RDKBDEV-3501 Fix resource leak in CosaUtilGetIpv6AddrInf… - #1481
Merged
Merged
Conversation
…o() (rdkcentral#1416) * RDKBDEV-3501: Fix resource leak in CosaUtilGetIpv6AddrInfo() Problem: The file opened using fopen() was not closed when memory reallocation failed or when the function completed successfully, resulting in a file resource leak. Fix: Close the opened file handle using fclose(fp) on the memory reallocation failure path and before the successful function return. Signed-off-by: Yashodha KB <yakshagowda37@gmail.com> * RDKBDEV-3501: Fix resource leak in CosaUtilGetIpv6AddrInfo Problem: The file opened using fopen() was not closed when memory reallocation failed or when the function completed successfully, resulting in a file resource leak. Fix: Close the opened file handle using fclose(fp) on the memory reallocation failure path and before the successful function return. Signed-off-by: Yashodha KB <yakshagowda37@gmail.com> * Apply batched suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --------- Signed-off-by: Yashodha KB <yakshagowda37@gmail.com> Co-authored-by: Narendra Varma Dandu <narendandu@gmail.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Signed-off-by: yashu <yakshagowda37@gmail.com>
Contributor
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
The affected allocation and file-handle paths are correctly handled without unrelated changes.
Review effort: Balanced
Findings: None
What changed in this PR
Fixes file and memory resource leaks in IPv6 address discovery.
Changes:
- Preserves the original allocation when
realloc()fails. - Closes
/proc/net/if_inet6on failure and success paths.
| File | Description |
|---|---|
source/dml/tr_181/ml/cosa_apis_util.c |
Corrects resource cleanup in CosaUtilGetIpv6AddrInfo(). |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
narendradandu
approved these changes
Oct 1, 2026
| (*p_num)++; | ||
| *pp_info = realloc(*pp_info, *p_num * sizeof(ipv6_addr_info_t)); | ||
| if (!*pp_info) | ||
| ipv6_addr_info_t *tmp = realloc(*pp_info, *p_num * sizeof(ipv6_addr_info_t)); |
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.

Issue:
In CosaUtilGetIpv6AddrInfo(), the /proc/net/if_inet6 file is opened using fopen(), but the file handle is not closed on certain return paths. If memory reallocation for pp_info fails, the function returns without closing the opened file handle. The file handle also remains open when the function completes successfully. This results in a file resource leak.
Additionally, realloc() is assigned directly to pp_info. If realloc() fails, the original memory pointer can be lost, resulting in a memory leak.
Steps to Reproduce:
Execute CosaUtilGetIpv6AddrInfo().
Allow the /proc/net/if_inet6 file to be opened successfully.
Trigger a memory reallocation failure, or allow the function to complete successfully.
Observe that the opened file handle is not closed on these paths.
Observe that the original pp_info allocation can be lost if realloc() fails.
Expected Behavior:
The opened file handle should be properly closed before returning from all paths after a successful fopen(). The original pp_info allocation should also be preserved when realloc() fails.
Fix:
Add fclose(fp) to the memory reallocation failure path and before the successful function return.
Use a temporary pointer for realloc() and update pp_info only after successful reallocation to prevent loss of the original allocation on failure.
Acceptance Criteria:
The opened file handle is properly closed when memory reallocation fails.
The opened file handle is properly closed before successful function return.
The original pp_info allocation is preserved if realloc() fails.
No file/resource or memory leak occurs on any affected path.
Existing successful execution and error-handling behavior remains unchanged.
No unnecessary changes are introduced to other code paths.