Protect against NULL device in the Android hidapi implementation

This commit is contained in:
Sam Lantinga 2019-06-07 09:00:26 -07:00
parent 9261e473d6
commit 23a2b47790
1 changed files with 65 additions and 42 deletions

View File

@ -1026,6 +1026,8 @@ 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) int HID_API_EXPORT HID_API_CALL hid_write(hid_device *device, const unsigned char *data, size_t length)
{
if ( device )
{ {
LOGV( "hid_write id=%d length=%u", device->m_nId, length ); LOGV( "hid_write id=%d length=%u", device->m_nId, length );
hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId ); hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
@ -1033,11 +1035,14 @@ int HID_API_EXPORT HID_API_CALL hid_write(hid_device *device, const unsigned ch
{ {
return pDevice->SendOutputReport( data, length ); return pDevice->SendOutputReport( data, length );
} }
}
return -1; // Controller was disconnected return -1; // Controller was disconnected
} }
// TODO: Implement timeout? // TODO: Implement timeout?
int HID_API_EXPORT HID_API_CALL hid_read_timeout(hid_device *device, unsigned char *data, size_t length, int milliseconds) int HID_API_EXPORT HID_API_CALL hid_read_timeout(hid_device *device, unsigned char *data, size_t length, int milliseconds)
{
if ( device )
{ {
// LOGV( "hid_read_timeout id=%d length=%u timeout=%d", device->m_nId, length, 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 ); hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
@ -1046,6 +1051,7 @@ int HID_API_EXPORT HID_API_CALL hid_read_timeout(hid_device *device, unsigned ch
return pDevice->GetInput( data, length ); return pDevice->GetInput( data, length );
} }
LOGV( "controller was disconnected" ); LOGV( "controller was disconnected" );
}
return -1; // Controller was disconnected return -1; // Controller was disconnected
} }
@ -1063,6 +1069,8 @@ 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) int HID_API_EXPORT HID_API_CALL hid_send_feature_report(hid_device *device, const unsigned char *data, size_t length)
{
if ( device )
{ {
LOGV( "hid_send_feature_report id=%d length=%u", device->m_nId, length ); LOGV( "hid_send_feature_report id=%d length=%u", device->m_nId, length );
hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId ); hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
@ -1070,12 +1078,15 @@ int HID_API_EXPORT HID_API_CALL hid_send_feature_report(hid_device *device, cons
{ {
return pDevice->SendFeatureReport( data, length ); return pDevice->SendFeatureReport( data, length );
} }
}
return -1; // Controller was disconnected return -1; // Controller was disconnected
} }
// Synchronous operation. Will block until completed. // 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) int HID_API_EXPORT HID_API_CALL hid_get_feature_report(hid_device *device, unsigned char *data, size_t length)
{
if ( device )
{ {
LOGV( "hid_get_feature_report id=%d length=%u", device->m_nId, length ); LOGV( "hid_get_feature_report id=%d length=%u", device->m_nId, length );
hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId ); hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
@ -1083,11 +1094,14 @@ int HID_API_EXPORT HID_API_CALL hid_get_feature_report(hid_device *device, unsig
{ {
return pDevice->GetFeatureReport( data, length ); return pDevice->GetFeatureReport( data, length );
} }
}
return -1; // Controller was disconnected return -1; // Controller was disconnected
} }
void HID_API_EXPORT HID_API_CALL hid_close(hid_device *device) void HID_API_EXPORT HID_API_CALL hid_close(hid_device *device)
{
if ( device )
{ {
LOGV( "hid_close id=%d", device->m_nId ); LOGV( "hid_close id=%d", device->m_nId );
hid_mutex_guard r( &g_DevicesRefCountMutex ); hid_mutex_guard r( &g_DevicesRefCountMutex );
@ -1105,10 +1119,12 @@ void HID_API_EXPORT HID_API_CALL hid_close(hid_device *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) int HID_API_EXPORT_CALL hid_get_manufacturer_string(hid_device *device, wchar_t *string, size_t maxlen)
{
if ( device )
{ {
hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId ); hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
if ( pDevice ) if ( pDevice )
@ -1116,10 +1132,13 @@ int HID_API_EXPORT_CALL hid_get_manufacturer_string(hid_device *device, wchar_t
wcsncpy( string, pDevice->GetDeviceInfo()->manufacturer_string, maxlen ); wcsncpy( string, pDevice->GetDeviceInfo()->manufacturer_string, maxlen );
return 0; return 0;
} }
}
return -1; return -1;
} }
int HID_API_EXPORT_CALL hid_get_product_string(hid_device *device, wchar_t *string, size_t maxlen) int HID_API_EXPORT_CALL hid_get_product_string(hid_device *device, wchar_t *string, size_t maxlen)
{
if ( device )
{ {
hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId ); hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
if ( pDevice ) if ( pDevice )
@ -1127,10 +1146,13 @@ int HID_API_EXPORT_CALL hid_get_product_string(hid_device *device, wchar_t *stri
wcsncpy( string, pDevice->GetDeviceInfo()->product_string, maxlen ); wcsncpy( string, pDevice->GetDeviceInfo()->product_string, maxlen );
return 0; return 0;
} }
}
return -1; return -1;
} }
int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *device, wchar_t *string, size_t maxlen) int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *device, wchar_t *string, size_t maxlen)
{
if ( device )
{ {
hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId ); hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
if ( pDevice ) if ( pDevice )
@ -1138,6 +1160,7 @@ int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *device, wchar_t
wcsncpy( string, pDevice->GetDeviceInfo()->serial_number, maxlen ); wcsncpy( string, pDevice->GetDeviceInfo()->serial_number, maxlen );
return 0; return 0;
} }
}
return -1; return -1;
} }