Skip to content

RDKCOM-5655: RDKBDEV-3501 Fix resource leak in CosaUtilGetIpv6AddrInf… - #1481

Merged
narendradandu merged 2 commits into
rdkcentral:mainfrom
tinaelizabeth84:RDKCOM-5655
Oct 1, 2026
Merged

narendradandu merged 2 commits into
rdkcentral:mainfrom
tinaelizabeth84:RDKCOM-5655

Conversation

@tinaelizabeth84

@tinaelizabeth84 tinaelizabeth84 commented Sep 30, 2026 •

Copy link
Copy Markdown

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.

…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>
Copilot AI balanced review requested due to automatic review settings September 30, 2026 18:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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_inet6 on 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.

Copilot AI balanced review requested due to automatic review settings October 1, 2026 20:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

The result count becomes inconsistent with the preserved allocation when reallocation fails.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)

(*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));
@narendradandu
narendradandu merged commit c06ea5b into rdkcentral:main Oct 1, 2026
4 of 5 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 1, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants