View Issue Details

IDProjectCategoryView StatusLast Update
0002671NoesisGUIUnity3Dpublic2023-08-23 18:19
Reporterckfinite Assigned Tosfernandez  
PrioritynormalSeverityminorReproducibilityalways
Status resolvedResolutionfixed 
Product Version3.2.1 
Target Version3.2.2Fixed in Version3.2.2 
Summary0002671: EventTrigger can fail with a nullreferenceexception without an explanatory error message
DescriptionThe implementation of EventTrigger's RegisterEvent does not gracefully handle the case where SourceObject is null but the given event does not exist on the source. In this case, the conditionals fall through and pass a null to IsValidEvent which chokes with a NullReferenceException. A more informative error message should be produced instead.
Steps To ReproduceThe implementation of EventTrigger's RegisterEvent method, as follows
[code]

        private void RegisterEvent(object source, string eventName)
        {
            if (source != null && !string.IsNullOrEmpty(eventName))
            {
                Type type = source.GetType();
                EventInfo ev = type.GetEvent(eventName);

                if (ev == null)
                {
                    if (SourceObject != null)
                    {
                        throw new ArgumentException(string.Format(
                            "EventTrigger cannot find event '{0}' in SourceObject '{1}'",
                            eventName, type));
                    }
                }

                if (!IsValidEvent(ev))
                {
                    if (SourceObject != null)
                    {
                        throw new ArgumentException(string.Format(
                            "SourceObject event '{0}' is not valid for EventTrigger",
                            eventName));
                    }
                }
                else
                {
                    _event = ev;
                    _handler = Delegate.CreateDelegate(ev.EventHandlerType, this, OnEventMethod);
                    _event.AddEventHandler(source, _handler);
                }
            }
        }
[/code]
doesn't properly handle the case where the default target object (usually AssociatedObject) exists but the designated event does not while no SourceObject is set (and thus SourceObject == null). As described above, this code will fall through with a null ev and null SourceObject and pass IsValidEvent null, which fails with a nullreferenceexception. An additional branch should be added to the condition to provide an informative message in this case.
TagsNo tags attached.
PlatformAny

Activities

sfernandez

sfernandez

2023-08-23 18:19

manager   ~0008661

Solved in r12660 with the following patch:

Index: EventTriggerBase.cs
===================================================================
--- EventTriggerBase.cs	(revision 12659)
+++ EventTriggerBase.cs	(revision 12660)
@@ -171,8 +171,7 @@
                             eventName, type));
                     }
                 }
-
-                if (!IsValidEvent(ev))
+                else if (!IsValidEvent(ev))
                 {
                     if (SourceObject != null)
                     {

Issue History

Date Modified Username Field Change
2023-08-22 06:40 ckfinite New Issue
2023-08-22 17:14 jsantos Assigned To => sfernandez
2023-08-22 17:14 jsantos Status new => assigned
2023-08-23 18:17 sfernandez Product Version 3.2 => 3.2.1
2023-08-23 18:17 sfernandez Target Version => 3.2.2
2023-08-23 18:19 sfernandez Status assigned => resolved
2023-08-23 18:19 sfernandez Resolution open => fixed
2023-08-23 18:19 sfernandez Fixed in Version => 3.2.2
2023-08-23 18:19 sfernandez Note Added: 0008661