Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,11 @@ private HRESULT HandleTaskDialogCallback(

case TASKDIALOG_NOTIFICATIONS.TDN_RADIO_BUTTON_CLICKED:
int radioButtonID = (int)wParam;
TaskDialogRadioButton radioButton = _boundPage.GetBoundRadioButtonByID(radioButtonID)!;
TaskDialogRadioButton? radioButton = _boundPage.GetBoundRadioButtonByID(radioButtonID);
if (radioButton is null)
{
break;
}

checked
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,10 @@ internal void DenyIfWaitingForInitialization()
// Check if the button is part of the custom buttons.
if (buttonID >= CustomButtonStartID)
{
return _boundCustomButtons![buttonID - CustomButtonStartID];
int customButtonIndex = buttonID - CustomButtonStartID;
return (uint)customButtonIndex < (uint)_boundCustomButtons!.Length
? _boundCustomButtons[customButtonIndex]
: null;
}
else
{
Expand All @@ -736,7 +739,15 @@ internal void DenyIfWaitingForInitialization()
throw new InvalidOperationException();
}

return buttonID == 0 ? null : _radioButtons[buttonID - RadioButtonStartID];
if (buttonID == 0)
{
return null;
}

int radioButtonIndex = buttonID - RadioButtonStartID;
return (uint)radioButtonIndex < (uint)_radioButtons.Count
? _radioButtons[radioButtonIndex]
: null;
}

internal void Validate()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#nullable disable

using Microsoft.DotNet.RemoteExecutor;
using System.Reflection;

namespace System.Windows.Forms.Tests;

Expand Down Expand Up @@ -58,4 +59,51 @@ public void TaskDialog_ShowDialog_SetProperty_DifferentThread_ThrowsInvalidOpera
// verify the remote process succeeded
Assert.Equal(RemoteExecutor.SuccessExitCode, invokerHandle.ExitCode);
}

[WinFormsFact]
public void TaskDialogPage_GetBoundButtonByID_CustomRangeOutOfBounds_ReturnsNull()
{
TaskDialogPage page = new();
PrepareBoundLikeState(page);
dynamic access = page.TestAccessor.Dynamic;
access._boundCustomButtons = Array.Empty<TaskDialogButton>();
access._boundStandardButtonsByID = new Dictionary<int, TaskDialogButton>();

TaskDialogButton button = page.GetBoundButtonByID(buttonID: 100);

Assert.Null(button);
}

[WinFormsFact]
public void TaskDialogPage_GetBoundRadioButtonByID_OutOfBounds_ReturnsNull()
{
TaskDialogPage page = new();
PrepareBoundLikeState(page);

TaskDialogRadioButton radioButton = page.GetBoundRadioButtonByID(buttonID: 1);

Assert.Null(radioButton);
}

private static void PrepareBoundLikeState(TaskDialogPage page)
{
ConstructorInfo constructor = typeof(TaskDialog).GetConstructor(
BindingFlags.Instance | BindingFlags.NonPublic,
binder: null,
Type.EmptyTypes,
modifiers: null);
Assert.NotNull(constructor);

TaskDialog dialog = (TaskDialog)constructor.Invoke(null);
SetPrivateField(page, "<BoundDialog>k__BackingField", dialog);
}

private static void SetPrivateField(object instance, string fieldName, object value)
{
FieldInfo field = instance.GetType().GetField(
fieldName,
BindingFlags.Instance | BindingFlags.NonPublic);
Assert.NotNull(field);
field.SetValue(instance, value);
}
}