Skip to content

Guard null-reference on methods[0].method in FindBestMethodImpl#27623

Open
euxaristia wants to merge 1 commit into
PowerShell:masterfrom
euxaristia:fix/nre-methods-array
Open

Guard null-reference on methods[0].method in FindBestMethodImpl#27623
euxaristia wants to merge 1 commit into
PowerShell:masterfrom
euxaristia:fix/nre-methods-array

Conversation

@euxaristia

Copy link
Copy Markdown

PR Summary

Guard against null-reference when accessing methods[0].method.Name in the error path of FindBestMethodImpl.

PR Context

When candidates.Count == 0 and the first condition (all methods are static generic type definitions) is false, the else-if branch on line 1639 accesses methods[0].method.Name. This can throw a NullReferenceException if methods[0].method is null or if methods.Length is 0. Add a null check and fall back to "<unknown>".

PR Checklist

  • PR title is meaningful and in present tense, imperative mood
  • Changes are summarized in the PR description
  • All files have the Microsoft copyright header
  • Ready for review -- this is not a Draft PR
  • Breaking changes: None
  • User-facing changes: No
  • Testing: N/A -- defensive null check only, no functional change

The error path in `FindBestMethodImpl` could dereference a null
`methods[0].method` when the `methods` array is empty or contains
entries with a null `method` field. Guard both conditions before
accessing `.Name`.
@euxaristia euxaristia requested a review from a team as a code owner June 21, 2026 22:09
Copilot AI review requested due to automatic review settings June 21, 2026 22:09

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.

Pull request overview

This PR adds a defensive guard in FindBestMethodImpl to prevent a null-reference when forming the generic-method-argument-count error message, falling back to "<unknown>" when the method name cannot be obtained.

Changes:

  • Introduce a safe methodName local computed from methods[0].method?.Name (with additional array-length guard).
  • Use the guarded methodName when formatting ExtendedTypeSystem.MethodGenericArgumentCountException.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants