Skip to content

Comments

Fix premature returns for argument/pointer checks in SOSDacImpl.cs#124814

Merged
rcj1 merged 4 commits intomainfrom
copilot/fix-argument-pointer-checks
Feb 25, 2026
Merged

Fix premature returns for argument/pointer checks in SOSDacImpl.cs#124814
rcj1 merged 4 commits intomainfrom
copilot/fix-argument-pointer-checks

Conversation

Copy link
Contributor

Copilot AI commented Feb 24, 2026

Description

Argument and pointer validation checks at the start of SOSDacImpl methods were returning early (before the try/catch), bypassing the catch block's HResult conversion and the #if DEBUG legacy comparison block.

Changes

  • E_INVALIDARG checks → moved inside try, replaced return HResults.E_INVALIDARG with throw new ArgumentException()
  • E_POINTER checks → moved inside try, replaced return HResults.E_POINTER with throw new NullReferenceException()
  • if/else guard patterns (ISOSDacInterface14 methods, GetILForModule, GetMethodDescFromToken, TraverseModuleMap) → flattened into unified try/catch with throws
  • Switch default: cases (GetMethodDescFromToken, TraverseModuleMap) → replaced hr = HResults.E_INVALIDARG with throw new ArgumentException() so execution halts rather than falling through
  • GetPEFileName → inner return HResults.E_NOTIMPL inside try replaced with throw new NotImplementedException() to prevent CopyStringToBuffer from running on empty paths
  • SOSMethodEnum.GetCount → added try/catch wrapper; return HResults.E_POINTERthrow new NullReferenceException()
  • GetThreadFromThinlockID → fixed latent bug: null check was setting hr but not preventing the try block from running, causing *pThread dereference on a null pointer

Before / After pattern:

// Before — bypasses catch and #if DEBUG block
int ISOSDacInterface.GetThreadData(ClrDataAddress thread, DacpThreadData* data)
{
    if (thread == 0 || data == null)
        return HResults.E_INVALIDARG;   // skips try/catch and DEBUG comparison

    int hr = HResults.S_OK;
    try { ... }
    catch (Exception ex) { hr = ex.HResult; }
    ...
}

// After — check participates in unified error handling
int ISOSDacInterface.GetThreadData(ClrDataAddress thread, DacpThreadData* data)
{
    int hr = HResults.S_OK;
    try
    {
        if (thread == 0 || data == null)
            throw new ArgumentException();
        ...
    }
    catch (Exception ex) { hr = ex.HResult; }
    ...
}

Testing

  • All 832 existing cDAC unit tests pass.

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @agocke
See info in area-owners.md if you want to be subscribed.

Copilot AI and others added 2 commits February 24, 2026 20:31
Co-authored-by: rcj1 <77995559+rcj1@users.noreply.github.com>
…x GetPEFileName E_NOTIMPL

Co-authored-by: rcj1 <77995559+rcj1@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix argument and pointer checks in SOSDacImpl.cs Fix premature returns for argument/pointer checks in SOSDacImpl.cs Feb 24, 2026
Copy link
Member

@max-charlamb max-charlamb left a comment

Choose a reason for hiding this comment

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

lgtm, nice cleanup.

Especially good find on the bug in GetThreadFromThinlockID

Co-authored-by: rcj1 <77995559+rcj1@users.noreply.github.com>
@rcj1 rcj1 marked this pull request as ready for review February 24, 2026 21:03
Copilot AI review requested due to automatic review settings February 24, 2026 21:03
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

This pull request refactors error handling in SOSDacImpl.cs by moving argument and pointer validation checks from before try blocks to inside try blocks, converting early returns to thrown exceptions. The goal is to ensure that the catch block can handle HResult conversion and that DEBUG comparison blocks work correctly.

Changes:

  • Moved null pointer and invalid argument checks from before try blocks to inside try blocks across approximately 30 methods
  • Changed early return HResults.E_INVALIDARG statements to throw new ArgumentException() inside try blocks
  • Changed early return HResults.E_POINTER statements to throw new NullReferenceException() inside try blocks
  • Restructured methods like GetILForModule, GetMethodDescFromToken, and TraverseModuleMap from if/else structures to unified try/catch blocks
  • Added try/catch wrapper to SOSMethodEnum.GetCount

@rcj1 rcj1 merged commit 9511672 into main Feb 25, 2026
56 of 58 checks passed
@rcj1 rcj1 deleted the copilot/fix-argument-pointer-checks branch February 25, 2026 03:51
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.

3 participants