From 2a84b9f02b679a7b31ff8420d6c5136aca8f8147 Mon Sep 17 00:00:00 2001 From: Marcus Tillmanns Date: Wed, 11 Oct 2023 12:41:33 +0200 Subject: [PATCH] Utils: Fix FileAccess if device returns nullptr A device may return a nullptr if it does not support device access. Change-Id: I302a2c63406268a2ff876c1bf408c5e26137fd9d Reviewed-by: Alessandro Portale --- src/libs/utils/filepath.cpp | 4 ++-- .../projectexplorer/devicesupport/devicemanager.cpp | 9 +++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/libs/utils/filepath.cpp b/src/libs/utils/filepath.cpp index a489db6c1a0..1a34bd038c8 100644 --- a/src/libs/utils/filepath.cpp +++ b/src/libs/utils/filepath.cpp @@ -1179,13 +1179,13 @@ DeviceFileAccess *FilePath::fileAccess() const static DeviceFileAccess dummy; const expected_str access = getFileAccess(*this); QTC_ASSERT_EXPECTED(access, return &dummy); - return *access ? *access : &dummy; + return *access; } bool FilePath::hasFileAccess() const { const expected_str access = getFileAccess(*this); - return access && access.value(); + return access.has_value(); } /*! diff --git a/src/plugins/projectexplorer/devicesupport/devicemanager.cpp b/src/plugins/projectexplorer/devicesupport/devicemanager.cpp index 57052d354f6..b06d5a14694 100644 --- a/src/plugins/projectexplorer/devicesupport/devicemanager.cpp +++ b/src/plugins/projectexplorer/devicesupport/devicemanager.cpp @@ -417,12 +417,17 @@ DeviceManager::DeviceManager(bool isInstance) : d(std::make_unique expected_str { if (!filePath.needsDevice()) return DesktopDeviceFileAccess::instance(); - auto device = DeviceManager::deviceForPath(filePath); + IDevice::ConstPtr device = DeviceManager::deviceForPath(filePath); if (!device) { return make_unexpected( QString("No device found for path \"%1\"").arg(filePath.toUserOutput())); } - return device->fileAccess(); + DeviceFileAccess *fileAccess = device->fileAccess(); + if (!fileAccess) { + return make_unexpected( + QString("No file access for device \"%1\"").arg(device->displayName())); + } + return fileAccess; }; deviceHooks.environment = [](const FilePath &filePath) -> expected_str {