Skip to content

Commit 5aa3d8d

Browse files
committed
fix(hid): Wrap event Windows HID handler logic inside of try/catch-block
1 parent dd18487 commit 5aa3d8d

File tree

1 file changed

+38
-26
lines changed

1 file changed

+38
-26
lines changed

Yubico.Core/src/Yubico/Core/Devices/Hid/WindowsHidDeviceListener.cs

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -113,40 +113,52 @@ private static int OnEventReceived(
113113
{
114114
var thisPtr = GCHandle.FromIntPtr(context);
115115
var thisObj = thisPtr.Target as WindowsHidDeviceListener;
116-
thisObj?._log.LogInformation("ConfigMgr callback received.");
117116

118-
CM_NOTIFY_EVENT_DATA eventData = Marshal.PtrToStructure<CM_NOTIFY_EVENT_DATA>(eventDataPtr);
119-
Debug.Assert(eventData.ClassGuid == CmInterfaceGuid.Hid);
120-
Debug.Assert(eventData.FilterType == CM_NOTIFY_FILTER_TYPE.DEVINTERFACE);
117+
try
118+
{
121119

122-
int stringOffset = 24; // Magic number from C land
123-
int stringSize = eventDataSize - stringOffset;
124-
byte[] buffer = new byte[eventDataSize];
120+
CM_NOTIFY_EVENT_DATA eventData = Marshal.PtrToStructure<CM_NOTIFY_EVENT_DATA>(eventDataPtr);
121+
Debug.Assert(eventData.ClassGuid == CmInterfaceGuid.Hid);
122+
Debug.Assert(eventData.FilterType == CM_NOTIFY_FILTER_TYPE.DEVINTERFACE);
125123

126-
Marshal.Copy(eventDataPtr + stringOffset, buffer, 0, stringSize);
124+
int stringOffset = 24; // Magic number from C land
125+
int stringSize = eventDataSize - stringOffset;
126+
byte[] buffer = new byte[eventDataSize];
127127

128-
switch (action)
129-
{
130-
case CM_NOTIFY_ACTION.DEVICEINTERFACEARRIVAL:
131-
{
132-
string instancePath = System.Text.Encoding.Unicode.GetString(buffer);
133-
var cmDevice = CmDevice.FromDevicePath(instancePath);
134-
if (cmDevice == null)
128+
Marshal.Copy(eventDataPtr + stringOffset, buffer, 0, stringSize);
129+
130+
switch (action)
131+
{
132+
case CM_NOTIFY_ACTION.DEVICEINTERFACEARRIVAL:
135133
{
136-
thisObj?._log.LogWarning("Failed to create CmDevice from instance path {InstancePath}.", instancePath);
137-
return 0; // We can't do anything without a CmDevice. Exit gracefully.
134+
string instancePath = System.Text.Encoding.Unicode.GetString(buffer);
135+
var cmDevice = CmDevice.FromDevicePath(instancePath);
136+
if (cmDevice == null)
137+
{
138+
thisObj?._log.LogWarning(
139+
"Failed to create CmDevice from instance path {InstancePath}.", instancePath);
140+
141+
return 0; // We can't do anything without a CmDevice. Exit gracefully.
142+
}
143+
144+
var device = new WindowsHidDevice(cmDevice);
145+
thisObj?.OnArrived(device);
146+
break;
138147
}
139-
140-
var device = new WindowsHidDevice(cmDevice);
141-
thisObj?.OnArrived(device);
148+
case CM_NOTIFY_ACTION.DEVICEINTERFACEREMOVAL:
149+
thisObj?.OnRemoved(NullDevice.Instance);
142150
break;
143-
}
144-
case CM_NOTIFY_ACTION.DEVICEINTERFACEREMOVAL:
145-
thisObj?.OnRemoved(NullDevice.Instance);
146-
break;
147-
}
151+
}
148152

149-
return 0;
153+
return 0;
154+
}
155+
catch (Exception ex)
156+
{
157+
// We must not let exceptions escape from this callback. There's nowhere for them to go, and
158+
// it will likely crash the process.
159+
thisObj?._log.LogDebug($"Exception in OnEventReceived: {ex}");
160+
return 0;
161+
}
150162
}
151163
}
152164
}

0 commit comments

Comments
 (0)