Skip to content

Remove redundant NULL check for pDir in ClrDataAccess::GetMetaDataFileInfoFromPEFile#125502

Open
Copilot wants to merge 2 commits intomainfrom
copilot/remove-redundant-null-check
Open

Remove redundant NULL check for pDir in ClrDataAccess::GetMetaDataFileInfoFromPEFile#125502
Copilot wants to merge 2 commits intomainfrom
copilot/remove-redundant-null-check

Conversation

Copy link
Contributor

Copilot AI commented Mar 12, 2026

pDir was declared as IMAGE_DATA_DIRECTORY *pDir = NULL and immediately tested with if (pDir == NULL || pDir->Size == 0) — always true, making the entire block dead code. Flagged by SVACE static analysis (Linux Verification Center).

Changes

  • src/coreclr/debug/daccess/daccess.cpp
    • Removed unused IMAGE_DATA_DIRECTORY *pDir local variable
    • Removed always-true if (pDir == NULL || pDir->Size == 0) wrapper, flattening the logic
    • Inlined pDir->Sizelayout->GetCorHeader()->MetaData.Size

Before:

IMAGE_DATA_DIRECTORY *pDir = NULL;
// ...
if (pDir == NULL || pDir->Size == 0)  // always true
{
    mdImage = pPEAssembly->GetPEImage();
    if (mdImage != NULL)
    {
        layout = mdImage->GetLoadedLayout();
        pDir = &layout->GetCorHeader()->MetaData;
        dwRvaHint = 0;
        dwDataSize = pDir->Size;
    }
    else { return false; }
}

After:

mdImage = pPEAssembly->GetPEImage();
if (mdImage != NULL)
{
    layout = mdImage->GetLoadedLayout();
    dwRvaHint = 0;
    dwDataSize = layout->GetCorHeader()->MetaData.Size;
}
else { return false; }

No semantic behavior change.

Original prompt

This section details on the original issue you should resolve

<issue_title>Redundant NULL check for pDir in ClrDataAccess::GetMetaDataFileInfoFromPEFile</issue_title>
<issue_description>### Description

In daccess.cpp, the variable pDir is initialized to NULL and immediately checked with if (pDir == NULL || pDir->Size == 0). Since pDir is always NULL at this point, the condition is always true and the check is redundant.

Reproduction Steps

See ClrDataAccess::GetMetaDataFileInfoFromPEFile in daccess.cpp:
cppIMAGE_DATA_DIRECTORY *pDir = NULL;
// ...
if (pDir == NULL || pDir->Size == 0) // always true

Expected behavior

The condition should either be removed or restructured to reflect actual intent.

Actual behavior

The if condition is always true because pDir is always NULL at that point. Static analysis tools flag this as unreachable/redundant code

Regression?

N/A

Known Workarounds

None

Configuration

this is a code quality issue, not runtime-specific.

Other information

Found by Linux Verification Center (linuxtesting.org) with SVACE.</issue_description>

<agent_instructions>Delete unnecessary check</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💬 Send tasks to Copilot coding agent from Slack and Teams to turn conversations into code. Copilot posts an update in your thread when it's finished.

Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix redundant NULL check for pDir in ClrDataAccess Remove redundant NULL check for pDir in ClrDataAccess::GetMetaDataFileInfoFromPEFile Mar 12, 2026
Copilot AI requested a review from jkotas March 12, 2026 16:43
@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @steveisok, @tommcdon, @dotnet/dotnet-diag
See info in area-owners.md if you want to be subscribed.

@jkotas jkotas marked this pull request as ready for review March 12, 2026 16:47
Copilot AI review requested due to automatic review settings March 12, 2026 16:47
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Removes dead code in ClrDataAccess::GetMetaDataFileInfoFromPEFile by eliminating an always-true pDir == NULL check and simplifying metadata size retrieval, addressing static analysis findings (SVACE).

Changes:

  • Removed redundant IMAGE_DATA_DIRECTORY* pDir local and the always-true if (pDir == NULL || ...) wrapper.
  • Simplified flow to directly fetch mdImage/layout and read MetaData.Size from the COR header.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Redundant NULL check for pDir in ClrDataAccess::GetMetaDataFileInfoFromPEFile

4 participants