From 0b6c67a3e3a1f0cfdc648cc6c360feba73ac7293 Mon Sep 17 00:00:00 2001 From: Richard Hughes Date: Thu, 25 Feb 2021 14:25:52 +0000 Subject: [PATCH] trivial: Set the GError when calling ioctl() on an un-opened device I've not yet worked out the real bug, but it was unexpected to crash the daemon when open failed, rather than for a critical misuse of the API. --- libfwupdplugin/fu-udev-device.c | 36 ++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/libfwupdplugin/fu-udev-device.c b/libfwupdplugin/fu-udev-device.c index d57f351d4..28b64bac6 100644 --- a/libfwupdplugin/fu-udev-device.c +++ b/libfwupdplugin/fu-udev-device.c @@ -1328,9 +1328,19 @@ fu_udev_device_ioctl (FuUdevDevice *self, g_return_val_if_fail (FU_IS_UDEV_DEVICE (self), FALSE); g_return_val_if_fail (request != 0x0, FALSE); g_return_val_if_fail (buf != NULL, FALSE); - g_return_val_if_fail (priv->fd > 0, FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE); + /* not open! */ + if (priv->fd == 0) { + g_set_error (error, + FWUPD_ERROR, + FWUPD_ERROR_INTERNAL, + "%s [%s] has not been opened", + fu_device_get_id (FU_DEVICE (self)), + fu_device_get_name (FU_DEVICE (self))); + return FALSE; + } + rc_tmp = ioctl (priv->fd, request, buf); if (rc != NULL) *rc = rc_tmp; @@ -1389,9 +1399,19 @@ fu_udev_device_pread_full (FuUdevDevice *self, goffset port, g_return_val_if_fail (FU_IS_UDEV_DEVICE (self), FALSE); g_return_val_if_fail (buf != NULL, FALSE); - g_return_val_if_fail (priv->fd > 0, FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE); + /* not open! */ + if (priv->fd == 0) { + g_set_error (error, + FWUPD_ERROR, + FWUPD_ERROR_INTERNAL, + "%s [%s] has not been opened", + fu_device_get_id (FU_DEVICE (self)), + fu_device_get_name (FU_DEVICE (self))); + return FALSE; + } + #ifdef HAVE_PWRITE if (pread (priv->fd, buf, bufsz, port) != (gssize) bufsz) { g_set_error (error, @@ -1434,9 +1454,19 @@ fu_udev_device_pwrite_full (FuUdevDevice *self, goffset port, FuUdevDevicePrivate *priv = GET_PRIVATE (self); g_return_val_if_fail (FU_IS_UDEV_DEVICE (self), FALSE); - g_return_val_if_fail (priv->fd > 0, FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE); + /* not open! */ + if (priv->fd == 0) { + g_set_error (error, + FWUPD_ERROR, + FWUPD_ERROR_INTERNAL, + "%s [%s] has not been opened", + fu_device_get_id (FU_DEVICE (self)), + fu_device_get_name (FU_DEVICE (self))); + return FALSE; + } + #ifdef HAVE_PWRITE if (pwrite (priv->fd, buf, bufsz, port) != (gssize) bufsz) { g_set_error (error,