diff options
| author | Simeon Kuran <[email protected]> | 2022-04-25 13:37:53 +0200 |
|---|---|---|
| committer | Simeon Kuran <[email protected]> | 2022-04-26 19:46:21 +0200 |
| commit | 64af542374c8b70a222b342aa1d33b5ed83e246a (patch) | |
| tree | ff9026b3b3ade8e3ae4faa46b80c26c9f2d909bc /src | |
| parent | 464418f5dee3965c3bfddb7303f68d3714c6a832 (diff) | |
Fix potential crash in QNetworkInformation if no network is available
In some circumstances, an empty QDBusInterface was created. However,
this can lead to a crash on some systems (observed in Ubuntu 20.04).
The crash happens, when no network is available.
Pick-to: 6.3
Change-Id: I37316db547f33f082b8aaa73494db1bdf5aded1d
Reviewed-by: MÃ¥rten Nordheim <[email protected]>
Diffstat (limited to 'src')
| -rw-r--r-- | src/plugins/networkinformation/networkmanager/qnetworkmanagerservice.cpp | 27 |
1 files changed, 16 insertions, 11 deletions
diff --git a/src/plugins/networkinformation/networkmanager/qnetworkmanagerservice.cpp b/src/plugins/networkinformation/networkmanager/qnetworkmanagerservice.cpp index 74d89d8f5db..908224673c5 100644 --- a/src/plugins/networkinformation/networkmanager/qnetworkmanagerservice.cpp +++ b/src/plugins/networkinformation/networkmanager/qnetworkmanagerservice.cpp @@ -119,20 +119,21 @@ QNetworkManagerInterface::NMConnectivityState QNetworkManagerInterface::connecti return QNetworkManagerInterface::NM_CONNECTIVITY_UNKNOWN; } -static QDBusInterface getPrimaryDevice(const QDBusObjectPath &devicePath) +static std::optional<QDBusInterface> getPrimaryDevice(const QDBusObjectPath &devicePath) { const QDBusInterface connection(NM_DBUS_SERVICE, devicePath.path(), NM_CONNECTION_DBUS_INTERFACE, QDBusConnection::systemBus()); if (!connection.isValid()) - return QDBusInterface({}, {}); + return std::nullopt; const auto devicePaths = connection.property("Devices").value<QList<QDBusObjectPath>>(); if (devicePaths.isEmpty()) - return QDBusInterface({}, {}); + return std::nullopt; const QDBusObjectPath primaryDevicePath = devicePaths.front(); - return QDBusInterface(NM_DBUS_SERVICE, primaryDevicePath.path(), NM_DEVICE_DBUS_INTERFACE, - QDBusConnection::systemBus()); + return std::make_optional<QDBusInterface>(NM_DBUS_SERVICE, primaryDevicePath.path(), + NM_DEVICE_DBUS_INTERFACE, + QDBusConnection::systemBus()); } std::optional<QDBusObjectPath> QNetworkManagerInterface::primaryConnectionDevicePath() const @@ -160,10 +161,12 @@ auto QNetworkManagerInterface::meteredState() const -> NMMetered auto QNetworkManagerInterface::extractDeviceType(const QDBusObjectPath &devicePath) const -> NMDeviceType { - QDBusInterface primaryDevice = getPrimaryDevice(devicePath); - if (!primaryDevice.isValid()) + const auto primaryDevice = getPrimaryDevice(devicePath); + if (!primaryDevice) return NM_DEVICE_TYPE_UNKNOWN; - const QVariant deviceType = primaryDevice.property("DeviceType"); + if (!primaryDevice->isValid()) + return NM_DEVICE_TYPE_UNKNOWN; + const QVariant deviceType = primaryDevice->property("DeviceType"); if (!deviceType.isValid()) return NM_DEVICE_TYPE_UNKNOWN; return static_cast<NMDeviceType>(deviceType.toUInt()); @@ -172,10 +175,12 @@ auto QNetworkManagerInterface::extractDeviceType(const QDBusObjectPath &devicePa auto QNetworkManagerInterface::extractDeviceMetered(const QDBusObjectPath &devicePath) const -> NMMetered { - QDBusInterface primaryDevice = getPrimaryDevice(devicePath); - if (!primaryDevice.isValid()) + const auto primaryDevice = getPrimaryDevice(devicePath); + if (!primaryDevice) + return NM_METERED_UNKNOWN; + if (!primaryDevice->isValid()) return NM_METERED_UNKNOWN; - const QVariant metered = primaryDevice.property("Metered"); + const QVariant metered = primaryDevice->property("Metered"); if (!metered.isValid()) return NM_METERED_UNKNOWN; return static_cast<NMMetered>(metered.toUInt()); |
