Fix segfault when parsing an invalid i2c device

Confusingly, g_regex_match_full() returns FALSE and doesn't always set
the GError. Just use g_path_get_basename() and g_str_has_prefix().
This commit is contained in:
Richard Hughes 2022-06-30 16:47:18 +01:00
parent 7b96c2e6cd
commit bb548f15f0

View File

@ -114,8 +114,7 @@ fu_i2c_device_probe(FuDevice *device, GError **error)
FuI2cDevicePrivate *priv = GET_PRIVATE(self);
GUdevDevice *udev_device = fu_udev_device_get_dev(FU_UDEV_DEVICE(device));
const gchar *tmp;
g_autoptr(GMatchInfo) info = NULL;
g_autoptr(GRegex) regex = NULL;
g_autofree gchar *devname = NULL;
#endif
/* set physical ID */
@ -130,13 +129,13 @@ fu_i2c_device_probe(FuDevice *device, GError **error)
return FALSE;
/* get bus number out of sysfs path */
regex = g_regex_new("/i2c-([0-9]+)/", 0, 0, error);
if (regex == NULL)
devname = g_path_get_basename(fu_udev_device_get_sysfs_path(FU_UDEV_DEVICE(device)));
if (g_str_has_prefix(devname, "i2c-")) {
guint64 tmp64 = 0;
if (!fu_strtoull(devname + 4, &tmp64, 0, G_MAXUINT, error))
return FALSE;
tmp = g_udev_device_get_sysfs_path(udev_device);
if (!g_regex_match_full(regex, tmp, -1, 0, 0, &info, error))
return FALSE;
priv->bus_number = g_ascii_strtoll(g_match_info_fetch(info, 1), NULL, 10);
priv->bus_number = tmp64;
}
#endif
/* success */