Fix issue 14831: TaskDialogPage.GetBoundButtonByID throws IndexOutOfRangeException when TaskDialogIndirect returns unexpected button ID - #14842
Open
SimonZhao888 wants to merge 3 commits into
Conversation
…angeException when TaskDialogIndirect returns unexpected button ID
Contributor
There was a problem hiding this comment.
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.GetBoundButtonByIDandGetBoundRadioButtonByID, returningnullwhen IDs are outside the configured range. - Guard the
TDN_RADIO_BUTTON_CLICKEDcallback against anullradio 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
PrepareBoundLikeStatecurrently 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-publicset_BoundDialogaccessor via the existingTestAccessorhelpers; doing so also lets you remove the now-unneededSetPrivateFieldhelper.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #14831
Root Cause
TaskDialogPage.GetBoundButtonByIDandGetBoundRadioButtonByIDmap the ID returned by the native API directly to an array index without performing bounds checking.If
TaskDialogIndirectreturns 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), anIndexOutOfRangeExceptionis triggered, preventing the intended fallback logic from executing.Proposed changes
custom-buttonindex inGetBoundButtonByID; returnnullif the index is out of bounds.radio-buttonindex inGetBoundRadioButtonByID; returnnullif the index is out of bounds.nullreturn value in theTaskDialogTDN_RADIO_BUTTON_CLICKEDcallback to prevent potential dereferencing issues.nullinstead of throwing an exception.Customer Impact
Regression?
Risk
Screenshots
Before
2026-08-04.142359.mp4
After
2026-08-04.142529.mp4
Test methodology
Test environment(s)
Test Project
TaskDialogIssue14831.zip
Microsoft Reviewers: Open in CodeFlow