thunderbolt: Fix a small memory leak at startup

This commit is contained in:
Richard Hughes 2021-10-27 13:55:45 +01:00
parent 758f34548c
commit 51a1bc5b66

View File

@ -32,7 +32,6 @@ struct _FuThunderboltDevice {
gboolean safe_mode; gboolean safe_mode;
gboolean is_native; gboolean is_native;
guint16 gen; guint16 gen;
gchar *devpath;
const gchar *auth_method; const gchar *auth_method;
}; };
@ -46,9 +45,10 @@ fu_thunderbolt_device_find_nvmem(FuThunderboltDevice *self, gboolean active, GEr
{ {
const gchar *nvmem_dir = active ? "nvm_active" : "nvm_non_active"; const gchar *nvmem_dir = active ? "nvm_active" : "nvm_non_active";
const gchar *name; const gchar *name;
const gchar *devpath = fu_udev_device_get_sysfs_path(FU_UDEV_DEVICE(self));
g_autoptr(GDir) d = NULL; g_autoptr(GDir) d = NULL;
if (G_UNLIKELY(self->devpath == NULL)) { if (G_UNLIKELY(devpath == NULL)) {
g_set_error_literal(error, g_set_error_literal(error,
FWUPD_ERROR, FWUPD_ERROR,
FWUPD_ERROR_INTERNAL, FWUPD_ERROR_INTERNAL,
@ -56,13 +56,13 @@ fu_thunderbolt_device_find_nvmem(FuThunderboltDevice *self, gboolean active, GEr
return NULL; return NULL;
} }
d = g_dir_open(self->devpath, 0, error); d = g_dir_open(devpath, 0, error);
if (d == NULL) if (d == NULL)
return NULL; return NULL;
while ((name = g_dir_read_name(d)) != NULL) { while ((name = g_dir_read_name(d)) != NULL) {
if (g_str_has_prefix(name, nvmem_dir)) { if (g_str_has_prefix(name, nvmem_dir)) {
g_autoptr(GFile) parent = g_file_new_for_path(self->devpath); g_autoptr(GFile) parent = g_file_new_for_path(devpath);
g_autoptr(GFile) nvm_dir = g_file_get_child(parent, name); g_autoptr(GFile) nvm_dir = g_file_get_child(parent, name);
return g_file_get_child(nvm_dir, "nvmem"); return g_file_get_child(nvm_dir, "nvmem");
} }
@ -111,8 +111,9 @@ fu_thunderbolt_device_check_authorized(FuThunderboltDevice *self, GError **error
guint64 status; guint64 status;
g_autofree gchar *attribute = NULL; g_autofree gchar *attribute = NULL;
const gchar *update_error = NULL; const gchar *update_error = NULL;
const gchar *devpath = fu_udev_device_get_sysfs_path(FU_UDEV_DEVICE(self));
/* read directly from file to prevent udev caching */ /* read directly from file to prevent udev caching */
g_autofree gchar *safe_path = g_build_path("/", self->devpath, "authorized", NULL); g_autofree gchar *safe_path = g_build_path("/", devpath, "authorized", NULL);
if (!g_file_test(safe_path, G_FILE_TEST_EXISTS)) { if (!g_file_test(safe_path, G_FILE_TEST_EXISTS)) {
g_set_error_literal(error, g_set_error_literal(error,
@ -160,11 +161,12 @@ fu_thunderbolt_device_can_update(FuThunderboltDevice *self)
static gboolean static gboolean
fu_thunderbolt_device_get_version(FuThunderboltDevice *self, GError **error) fu_thunderbolt_device_get_version(FuThunderboltDevice *self, GError **error)
{ {
const gchar *devpath = fu_udev_device_get_sysfs_path(FU_UDEV_DEVICE(self));
g_auto(GStrv) split = NULL; g_auto(GStrv) split = NULL;
g_autofree gchar *version_raw = NULL; g_autofree gchar *version_raw = NULL;
g_autofree gchar *version = NULL; g_autofree gchar *version = NULL;
/* read directly from file to prevent udev caching */ /* read directly from file to prevent udev caching */
g_autofree gchar *safe_path = g_build_path("/", self->devpath, "nvm_version", NULL); g_autofree gchar *safe_path = g_build_path("/", devpath, "nvm_version", NULL);
if (!g_file_test(safe_path, G_FILE_TEST_EXISTS)) { if (!g_file_test(safe_path, G_FILE_TEST_EXISTS)) {
g_set_error_literal(error, g_set_error_literal(error,
@ -211,12 +213,13 @@ fu_thunderbolt_device_get_version(FuThunderboltDevice *self, GError **error)
static void static void
fu_thunderbolt_device_check_safe_mode(FuThunderboltDevice *self) fu_thunderbolt_device_check_safe_mode(FuThunderboltDevice *self)
{ {
const gchar *devpath = fu_udev_device_get_sysfs_path(FU_UDEV_DEVICE(self));
/* failed to read, for host check for safe mode */ /* failed to read, for host check for safe mode */
if (self->device_type != FU_THUNDERBOLT_DEVICE_TYPE_DEVICE_CONTROLLER) if (self->device_type != FU_THUNDERBOLT_DEVICE_TYPE_DEVICE_CONTROLLER)
return; return;
g_warning("%s is in safe mode -- VID/DID will " g_warning("%s is in safe mode -- VID/DID will "
"need to be set by another plugin", "need to be set by another plugin",
self->devpath); devpath);
self->safe_mode = TRUE; self->safe_mode = TRUE;
fu_device_set_version(FU_DEVICE(self), "00.00"); fu_device_set_version(FU_DEVICE(self), "00.00");
fu_device_add_instance_id(FU_DEVICE(self), "TBT-safemode"); fu_device_add_instance_id(FU_DEVICE(self), "TBT-safemode");
@ -266,13 +269,12 @@ fu_thunderbolt_device_probe(FuDevice *device, GError **error)
{ {
FuThunderboltDevice *self = FU_THUNDERBOLT_DEVICE(device); FuThunderboltDevice *self = FU_THUNDERBOLT_DEVICE(device);
const gchar *tmp = fu_udev_device_get_devtype(FU_UDEV_DEVICE(device)); const gchar *tmp = fu_udev_device_get_devtype(FU_UDEV_DEVICE(device));
const gchar *devpath = fu_udev_device_get_sysfs_path(FU_UDEV_DEVICE(device));
/* FuUdevDevice->probe */ /* FuUdevDevice->probe */
if (!FU_DEVICE_CLASS(fu_thunderbolt_device_parent_class)->probe(device, error)) if (!FU_DEVICE_CLASS(fu_thunderbolt_device_parent_class)->probe(device, error))
return FALSE; return FALSE;
self->devpath = g_strdup(fu_udev_device_get_sysfs_path(FU_UDEV_DEVICE(device)));
/* device */ /* device */
if (g_strcmp0(tmp, "thunderbolt_device") == 0) { if (g_strcmp0(tmp, "thunderbolt_device") == 0) {
g_autofree gchar *parent_name = g_autofree gchar *parent_name =
@ -286,7 +288,7 @@ fu_thunderbolt_device_probe(FuDevice *device, GError **error)
/* retimer */ /* retimer */
} else if (g_strcmp0(tmp, "thunderbolt_retimer") == 0) { } else if (g_strcmp0(tmp, "thunderbolt_retimer") == 0) {
self->device_type = FU_THUNDERBOLT_DEVICE_TYPE_RETIMER; self->device_type = FU_THUNDERBOLT_DEVICE_TYPE_RETIMER;
tmp = g_path_get_basename(self->devpath); tmp = g_path_get_basename(devpath);
if (tmp != NULL) if (tmp != NULL)
fu_device_set_physical_id(device, tmp); fu_device_set_physical_id(device, tmp);
/* domain or unsupported */ /* domain or unsupported */
@ -369,8 +371,9 @@ fu_thunderbolt_device_setup_controller(FuDevice *device, GError **error)
g_autofree gchar *device_id = NULL; g_autofree gchar *device_id = NULL;
g_autofree gchar *domain_id = NULL; g_autofree gchar *domain_id = NULL;
if (fu_thunderbolt_device_can_update(self)) { if (fu_thunderbolt_device_can_update(self)) {
const gchar *devpath = fu_udev_device_get_sysfs_path(FU_UDEV_DEVICE(self));
g_autofree gchar *vendor_id = NULL; g_autofree gchar *vendor_id = NULL;
g_autofree gchar *domain = g_path_get_basename(self->devpath); g_autofree gchar *domain = g_path_get_basename(devpath);
/* USB4 controllers don't have a concept of legacy vs native /* USB4 controllers don't have a concept of legacy vs native
* so don't try to read a native attribute from their NVM */ * so don't try to read a native attribute from their NVM */
if (self->device_type == FU_THUNDERBOLT_DEVICE_TYPE_HOST_CONTROLLER && if (self->device_type == FU_THUNDERBOLT_DEVICE_TYPE_HOST_CONTROLLER &&
@ -481,8 +484,6 @@ fu_thunderbolt_device_setup(FuDevice *device, GError **error)
FuThunderboltDevice *self = FU_THUNDERBOLT_DEVICE(device); FuThunderboltDevice *self = FU_THUNDERBOLT_DEVICE(device);
g_autoptr(GError) error_version = NULL; g_autoptr(GError) error_version = NULL;
self->devpath = g_strdup(fu_udev_device_get_sysfs_path(FU_UDEV_DEVICE(device)));
/* try to read the version */ /* try to read the version */
if (!fu_thunderbolt_device_get_version(self, &error_version)) { if (!fu_thunderbolt_device_get_version(self, &error_version)) {
if (self->device_type != FU_THUNDERBOLT_DEVICE_TYPE_HOST_CONTROLLER && if (self->device_type != FU_THUNDERBOLT_DEVICE_TYPE_HOST_CONTROLLER &&
@ -739,7 +740,7 @@ fu_thunderbolt_device_write_firmware(FuDevice *device,
if (!fu_thunderbolt_device_write_data(self, blob_fw, progress, error)) { if (!fu_thunderbolt_device_write_data(self, blob_fw, progress, error)) {
g_prefix_error(error, g_prefix_error(error,
"could not write firmware to thunderbolt device at %s: ", "could not write firmware to thunderbolt device at %s: ",
self->devpath); fu_udev_device_get_sysfs_path(FU_UDEV_DEVICE(self)));
return FALSE; return FALSE;
} }
@ -792,20 +793,10 @@ fu_thunderbolt_device_init(FuThunderboltDevice *self)
fu_device_set_version_format(FU_DEVICE(self), FWUPD_VERSION_FORMAT_PAIR); fu_device_set_version_format(FU_DEVICE(self), FWUPD_VERSION_FORMAT_PAIR);
} }
static void
fu_thunderbolt_device_finalize(GObject *object)
{
FuThunderboltDevice *self = FU_THUNDERBOLT_DEVICE(object);
G_OBJECT_CLASS(fu_thunderbolt_device_parent_class)->finalize(object);
g_free(self->devpath);
}
static void static void
fu_thunderbolt_device_class_init(FuThunderboltDeviceClass *klass) fu_thunderbolt_device_class_init(FuThunderboltDeviceClass *klass)
{ {
GObjectClass *object_class = G_OBJECT_CLASS(klass);
FuDeviceClass *klass_device = FU_DEVICE_CLASS(klass); FuDeviceClass *klass_device = FU_DEVICE_CLASS(klass);
object_class->finalize = fu_thunderbolt_device_finalize;
klass_device->activate = fu_thunderbolt_device_activate; klass_device->activate = fu_thunderbolt_device_activate;
klass_device->to_string = fu_thunderbolt_device_to_string; klass_device->to_string = fu_thunderbolt_device_to_string;
klass_device->setup = fu_thunderbolt_device_setup; klass_device->setup = fu_thunderbolt_device_setup;