Skip to content

Commit fda6040

Browse files
YourRobotOverlordCopilottig
authored
Fixes NullReferenceException in AdornmentView.Contains when Adornment is null (#4884)
AdornmentView has a parameter-less constructor (for AllViewsTester reflection support) that leaves Adornment = null. When such a view is placed inside a SuperView (e.g. the Themes UICatalog scenario creates AdornmentView/MarginView via reflection), moving the mouse over it caused a NullReferenceException at Adornment!.Thickness.Contains(...) because the null-forgiving ! operator did not prevent the dereference. Fix: add a guard clause returning false when Adornment is null, matching the existing pattern used in OnClearingViewport, OnDrawingText, and OnDrawingSubViews. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Tig <tig@users.noreply.github.com>
1 parent 9d40ced commit fda6040

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

Terminal.Gui/ViewBase/Adornment/AdornmentView.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,15 @@ public override bool Contains (in Point location)
190190
}
191191
}
192192

193+
if (Adornment is null)
194+
{
195+
return false;
196+
}
197+
193198
Rectangle outside = Frame;
194199
outside.Offset (parentOrSuperView.Frame.Location);
195200

196-
return Adornment!.Thickness.Contains (outside, location);
201+
return Adornment.Thickness.Contains (outside, location);
197202
}
198203

199204
#endregion View Overrides
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace ViewBaseTests.Adornments;
2+
3+
// Copilot
4+
public class AdornmentViewTests
5+
{
6+
/// <summary>
7+
/// Regression test for https://github.com/gui-cs/Terminal.Gui/issues/4883.
8+
/// AdornmentView.Contains must not throw NullReferenceException when Adornment is null
9+
/// (i.e. the view was created via its parameter-less constructor, as happens when
10+
/// AllViewsTester / Themes UICatalog scenario creates it via reflection).
11+
/// </summary>
12+
[Fact]
13+
public void Contains_Returns_False_When_Adornment_Is_Null ()
14+
{
15+
// Arrange - parameter-less ctor leaves Adornment = null
16+
AdornmentView adornmentView = new ();
17+
18+
// Act & Assert - must not throw NullReferenceException
19+
bool result = adornmentView.Contains (new Point (0, 0));
20+
21+
Assert.False (result);
22+
}
23+
24+
25+
}

0 commit comments

Comments
 (0)