Protect against NULL device in the Android hidapi implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
diff --git a/src/hidapi/android/hid.cpp b/src/hidapi/android/hid.cpp
index 7b8d41c..df82dcc 100644
--- a/src/hidapi/android/hid.cpp
+++ b/src/hidapi/android/hid.cpp
@@ -1027,11 +1027,14 @@ HID_API_EXPORT hid_device * HID_API_CALL hid_open_path(const char *path, int bEx
int HID_API_EXPORT HID_API_CALL hid_write(hid_device *device, const unsigned char *data, size_t length)
{
- LOGV( "hid_write id=%d length=%u", device->m_nId, length );
- hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
- if ( pDevice )
+ if ( device )
{
- return pDevice->SendOutputReport( data, length );
+ LOGV( "hid_write id=%d length=%u", device->m_nId, length );
+ hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
+ if ( pDevice )
+ {
+ return pDevice->SendOutputReport( data, length );
+ }
}
return -1; // Controller was disconnected
}
@@ -1039,13 +1042,16 @@ int HID_API_EXPORT HID_API_CALL hid_write(hid_device *device, const unsigned ch
// TODO: Implement timeout?
int HID_API_EXPORT HID_API_CALL hid_read_timeout(hid_device *device, unsigned char *data, size_t length, int milliseconds)
{
-// LOGV( "hid_read_timeout id=%d length=%u timeout=%d", device->m_nId, length, milliseconds );
- hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
- if ( pDevice )
+ if ( device )
{
- return pDevice->GetInput( data, length );
+// LOGV( "hid_read_timeout id=%d length=%u timeout=%d", device->m_nId, length, milliseconds );
+ hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
+ if ( pDevice )
+ {
+ return pDevice->GetInput( data, length );
+ }
+ LOGV( "controller was disconnected" );
}
- LOGV( "controller was disconnected" );
return -1; // Controller was disconnected
}
@@ -1064,11 +1070,14 @@ int HID_API_EXPORT HID_API_CALL hid_set_nonblocking(hid_device *device, int non
int HID_API_EXPORT HID_API_CALL hid_send_feature_report(hid_device *device, const unsigned char *data, size_t length)
{
- LOGV( "hid_send_feature_report id=%d length=%u", device->m_nId, length );
- hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
- if ( pDevice )
+ if ( device )
{
- return pDevice->SendFeatureReport( data, length );
+ LOGV( "hid_send_feature_report id=%d length=%u", device->m_nId, length );
+ hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
+ if ( pDevice )
+ {
+ return pDevice->SendFeatureReport( data, length );
+ }
}
return -1; // Controller was disconnected
}
@@ -1077,11 +1086,14 @@ int HID_API_EXPORT HID_API_CALL hid_send_feature_report(hid_device *device, cons
// Synchronous operation. Will block until completed.
int HID_API_EXPORT HID_API_CALL hid_get_feature_report(hid_device *device, unsigned char *data, size_t length)
{
- LOGV( "hid_get_feature_report id=%d length=%u", device->m_nId, length );
- hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
- if ( pDevice )
+ if ( device )
{
- return pDevice->GetFeatureReport( data, length );
+ LOGV( "hid_get_feature_report id=%d length=%u", device->m_nId, length );
+ hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
+ if ( pDevice )
+ {
+ return pDevice->GetFeatureReport( data, length );
+ }
}
return -1; // Controller was disconnected
}
@@ -1089,54 +1101,65 @@ int HID_API_EXPORT HID_API_CALL hid_get_feature_report(hid_device *device, unsig
void HID_API_EXPORT HID_API_CALL hid_close(hid_device *device)
{
- LOGV( "hid_close id=%d", device->m_nId );
- hid_mutex_guard r( &g_DevicesRefCountMutex );
- LOGD("Decrementing device %d (%p), refCount = %d\n", device->m_nId, device, device->m_nDeviceRefCount - 1);
- if ( --device->m_nDeviceRefCount == 0 )
+ if ( device )
{
- hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
- if ( pDevice )
- {
- pDevice->Close( true );
- }
- else
+ LOGV( "hid_close id=%d", device->m_nId );
+ hid_mutex_guard r( &g_DevicesRefCountMutex );
+ LOGD("Decrementing device %d (%p), refCount = %d\n", device->m_nId, device, device->m_nDeviceRefCount - 1);
+ if ( --device->m_nDeviceRefCount == 0 )
{
- delete device;
+ hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
+ if ( pDevice )
+ {
+ pDevice->Close( true );
+ }
+ else
+ {
+ delete device;
+ }
+ LOGD("Deleted device %p\n", device);
}
- LOGD("Deleted device %p\n", device);
}
-
}
int HID_API_EXPORT_CALL hid_get_manufacturer_string(hid_device *device, wchar_t *string, size_t maxlen)
{
- hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
- if ( pDevice )
+ if ( device )
{
- wcsncpy( string, pDevice->GetDeviceInfo()->manufacturer_string, maxlen );
- return 0;
+ hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
+ if ( pDevice )
+ {
+ wcsncpy( string, pDevice->GetDeviceInfo()->manufacturer_string, maxlen );
+ return 0;
+ }
}
return -1;
}
int HID_API_EXPORT_CALL hid_get_product_string(hid_device *device, wchar_t *string, size_t maxlen)
{
- hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
- if ( pDevice )
+ if ( device )
{
- wcsncpy( string, pDevice->GetDeviceInfo()->product_string, maxlen );
- return 0;
+ hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
+ if ( pDevice )
+ {
+ wcsncpy( string, pDevice->GetDeviceInfo()->product_string, maxlen );
+ return 0;
+ }
}
return -1;
}
int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *device, wchar_t *string, size_t maxlen)
{
- hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
- if ( pDevice )
+ if ( device )
{
- wcsncpy( string, pDevice->GetDeviceInfo()->serial_number, maxlen );
- return 0;
+ hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
+ if ( pDevice )
+ {
+ wcsncpy( string, pDevice->GetDeviceInfo()->serial_number, maxlen );
+ return 0;
+ }
}
return -1;
}