Skip to content

Fix issue 14831: TaskDialogPage.GetBoundButtonByID throws IndexOutOfRangeException when TaskDialogIndirect returns unexpected button ID - #14842

Open
SimonZhao888 wants to merge 3 commits into
dotnet:mainfrom
SimonZhao888:Fix_Issue_14831
Open

Fix issue 14831: TaskDialogPage.GetBoundButtonByID throws IndexOutOfRangeException when TaskDialogIndirect returns unexpected button ID#14842
SimonZhao888 wants to merge 3 commits into
dotnet:mainfrom
SimonZhao888:Fix_Issue_14831

Conversation

@SimonZhao888

@SimonZhao888 SimonZhao888 commented Aug 4, 2026

Copy link
Copy Markdown
Member

Fixes #14831

Root Cause

TaskDialogPage.GetBoundButtonByID and GetBoundRadioButtonByID map the ID returned by the native API directly to an array index without performing bounds checking.
If TaskDialogIndirect returns an ID outside the current page configuration during an abnormal closure (e.g., an ID in the "custom" range when no custom buttons are defined), an IndexOutOfRangeException is triggered, preventing the intended fallback logic from executing.

Proposed changes

  • Add bounds checking for the custom-button index in GetBoundButtonByID; return null if the index is out of bounds.
  • Add bounds checking for the radio-button index in GetBoundRadioButtonByID; return null if the index is out of bounds.
  • Handle the null return value in the TaskDialog TDN_RADIO_BUTTON_CLICKED callback to prevent potential dereferencing issues.
  • Add unit tests to cover the scenario where an out-of-bounds ID returns null instead of throwing an exception.

Customer Impact

  • Eliminated the risk of process crashes (IndexOutOfRangeException) in this code path.
  • Improved the robustness of TaskDialog when encountering exceptional or unexpected native return values, ensuring it correctly enters existing fault-tolerance branches.
  • Maintained the behavior of standard execution paths and public APIs, focusing solely on enhancing reliability and fault tolerance.

Regression?

  • No

Risk

  • Mini

Screenshots

Before

2026-08-04.142359.mp4

After

2026-08-04.142529.mp4

Test methodology

  • Manually
  • Automated test cases

Test environment(s)

  • 11.0.0-preview.7.26381.103

Test Project

TaskDialogIssue14831.zip

Microsoft Reviewers: Open in CodeFlow

…angeException when TaskDialogIndirect returns unexpected button ID

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

Improves the robustness of WinForms TaskDialog by ensuring unexpected native button/radio IDs don’t cause IndexOutOfRangeException (or subsequent null dereferences), allowing existing fallback behavior to run as intended.

Changes:

  • Add bounds checks in TaskDialogPage.GetBoundButtonByID and GetBoundRadioButtonByID, returning null when IDs are outside the configured range.
  • Guard the TDN_RADIO_BUTTON_CLICKED callback against a null radio button mapping.
  • Add unit tests covering out-of-bounds ID scenarios (with some test-side reflection setup).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/System.Windows.Forms/System/Windows/Forms/Dialogs/TaskDialog/TaskDialogPage.cs Adds safe bounds checks for custom/radio button ID mapping to prevent IndexOutOfRangeException.
src/System.Windows.Forms/System/Windows/Forms/Dialogs/TaskDialog/TaskDialog.cs Avoids dereferencing a null radio button when an unexpected ID is received in the native callback.
src/test/unit/System.Windows.Forms/System/Windows/Forms/TaskDialogTests.cs Adds regression tests for out-of-bounds IDs (currently using reflection-based setup).
Suppressed comments (1)

src/test/unit/System.Windows.Forms/System/Windows/Forms/TaskDialogTests.cs:91

  • PrepareBoundLikeState currently sets the auto-property backing field name (<BoundDialog>k__BackingField) via reflection. This is fragile (compiler-generated name) and can be avoided by invoking the non-public set_BoundDialog accessor via the existing TestAccessor helpers; doing so also lets you remove the now-unneeded SetPrivateField helper.
    private static void PrepareBoundLikeState(TaskDialogPage page)
    {
        ConstructorInfo constructor = typeof(TaskDialog).GetConstructor(
            BindingFlags.Instance | BindingFlags.NonPublic,
            binder: null,

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.

TaskDialogPage.GetBoundButtonByID throws IndexOutOfRangeException when TaskDialogIndirect returns unexpected button ID

2 participants