[PATCH v3] kernel-device: use device sysfs if physdev sysfs isn't available

Ben Chan benchan at chromium.org
Thu Mar 2 18:32:11 UTC 2017


find_device_by_physdev_uid() expects a non-NULL UID of the physical
device. However, mm_kernel_device_get_physdev_uid() could potentially
return NULL on MMKernelDeviceUdev if find_physical_gudevdevice() in
mm-kernel-device-udev.c fails to find the physical device. When a NULL
physical device UID is passed to find_device_by_physdev_uid() and used
in g_hash_table_lookup(), it leads to a crash in g_str_hash(), which is
a bit obscure.

This patch updates kernel_device_get_physdev_uid() in
mm-kernel-device-udev.c to fall back to use the device sysfs if the
physical device sysfs isn't available, which is similar to how
kernel_device_get_physdev_uid() mm-kernel-device-generic.c is
implemented.
---
 src/kerneldevice/mm-kernel-device-udev.c | 28 +++++++++++++++++++---------
 1 file changed, 19 insertions(+), 9 deletions(-)

diff --git a/src/kerneldevice/mm-kernel-device-udev.c b/src/kerneldevice/mm-kernel-device-udev.c
index ab75aa34..dc47cd63 100644
--- a/src/kerneldevice/mm-kernel-device-udev.c
+++ b/src/kerneldevice/mm-kernel-device-udev.c
@@ -347,19 +347,29 @@ kernel_device_get_physdev_uid (MMKernelDevice *_self)
     self = MM_KERNEL_DEVICE_UDEV (_self);
 
     /* Prefer the one coming in the properties, if any */
-    if (self->priv->properties)
-        uid = mm_kernel_event_properties_get_uid (MM_KERNEL_DEVICE_UDEV (self)->priv->properties);
+    if (self->priv->properties) {
+        if ((uid = mm_kernel_event_properties_get_uid (self->priv->properties)) != NULL)
+            return uid;
+    }
 
-    if (!uid) {
-        ensure_physdev (self);
-        if (!self->priv->physdev)
-            return NULL;
+    ensure_physdev (self);
+    if (self->priv->physdev) {
+        /* Try to load from properties set on the physical device */
+        if ((uid = g_udev_device_get_property (self->priv->physdev, "ID_MM_PHYSDEV_UID")) != NULL)
+            return uid;
+
+        /* Use physical device sysfs path, if any */
+        if ((uid = g_udev_device_get_sysfs_path (self->priv->physdev)) != NULL)
+            return uid;
+    }
 
-        uid = g_udev_device_get_property (self->priv->physdev, "ID_MM_PHYSDEV_UID");
-        if (!uid)
-            uid = g_udev_device_get_sysfs_path (self->priv->physdev);
+    if (self->priv->device) {
+        /* If there is no physical device sysfs path, use the device sysfs itself */
+        uid = g_udev_device_get_sysfs_path (self->priv->device);
     }
 
+    /* The caller of this function expects a non-NULL UID */
+    g_assert (uid != NULL);
     return uid;
 }
 
-- 
2.12.0.rc1.440.g5b76565f74-goog



More information about the ModemManager-devel mailing list