From 9d2cee79fe6e30a5558d15d12d2cbb16c9ae613c Mon Sep 17 00:00:00 2001 From: ComixHe Date: Sun, 18 Feb 2024 13:11:57 +0800 Subject: [PATCH] refact: suppress warnings and standardize project Signed-off-by: ComixHe --- CMakeLists.txt | 5 +++ apps/app-launch-helper/src/main.cpp | 6 ++-- src/cgroupsidentifier.cpp | 2 +- src/dbus/applicationmanager1service.cpp | 21 +++++++----- src/dbus/applicationservice.cpp | 43 ++++++++++++++++--------- src/dbus/applicationservice.h | 2 +- src/dbus/instanceservice.cpp | 5 ++- src/dbus/instanceservice.h | 2 +- src/dbus/jobmanager1service.cpp | 5 +-- src/dbus/jobmanager1service.h | 11 +++++-- src/dbus/mimemanager1service.cpp | 4 +-- src/dbus/processguesser1service.cpp | 8 ++--- src/desktopfileparser.cpp | 2 +- src/launchoptions.cpp | 1 - 14 files changed, 71 insertions(+), 46 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index acb23a0..dacab38 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,12 +7,17 @@ project(dde-application-manager ) set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_C_STANDARD 11) +set(CMAKE_C_STANDARD_REQUIRED ON) set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(THREADS_PREFER_PTHREAD_FLAG ON) +add_definitions(-Wall -Wextra -Wpedantic -Wformat) + set(BUILD_EXAMPLES OFF CACHE BOOL "Whether to build examples or not.") set(DDE_AM_USE_DEBUG_DBUS_NAME OFF CACHE BOOL "build a dbus service using a different bus name for debug.") set(PROFILING_MODE OFF CACHE BOOL "run a valgrind performance profiling.") diff --git a/apps/app-launch-helper/src/main.cpp b/apps/app-launch-helper/src/main.cpp index ed7fda7..648b136 100644 --- a/apps/app-launch-helper/src/main.cpp +++ b/apps/app-launch-helper/src/main.cpp @@ -73,9 +73,7 @@ int processExecStart(msg_ptr &msg, const std::deque &execArgs) if (ret = sd_bus_message_open_container(msg, SD_BUS_TYPE_VARIANT, "a(sasb)"); ret < 0) { sd_journal_perror("open variant of execStart failed."); - if (auto tmp = sd_bus_message_close_container(msg)) { - return ret; - } + return ret; } if (ret = sd_bus_message_open_container(msg, SD_BUS_TYPE_ARRAY, "(sasb)"); ret < 0) { @@ -229,7 +227,7 @@ std::string cmdParse(msg_ptr &msg, std::deque cmdLines) } auto kvStr = str.substr(2); - if (!kvStr.empty()) [[likely]] { + if (!kvStr.empty()) { const auto *it = kvStr.cbegin(); if (it = std::find(it, kvStr.cend(), '='); it == kvStr.cend()) { sd_journal_print(LOG_WARNING, "invalid k-v pair: %s", kvStr.data()); diff --git a/src/cgroupsidentifier.cpp b/src/cgroupsidentifier.cpp index 0d62ed2..ac33b8a 100644 --- a/src/cgroupsidentifier.cpp +++ b/src/cgroupsidentifier.cpp @@ -82,7 +82,7 @@ QString CGroupsIdentifier::parseCGroupsPath(QFile &cgroupFile) noexcept return {}; } - if (auto processUID = processUIDStr.toInt(); processUID != getCurrentUID()) { + if (auto processUID = processUIDStr.toInt(); static_cast(processUID) != getCurrentUID()) { qWarning() << "process is not in CGroups of current user, ignore...."; return {}; } diff --git a/src/dbus/applicationmanager1service.cpp b/src/dbus/applicationmanager1service.cpp index f885168..98badc7 100644 --- a/src/dbus/applicationmanager1service.cpp +++ b/src/dbus/applicationmanager1service.cpp @@ -32,7 +32,10 @@ void ApplicationManager1Service::initService(QDBusConnection &connection) noexce qFatal("%s", connection.lastError().message().toLocal8Bit().data()); } - new (std::nothrow) ProcessGuesser1Service{connection, this}; + if (auto *tmp = new (std::nothrow) ProcessGuesser1Service{connection, this}; tmp == nullptr) { + qCritical() << "new ProcessGuesser1Service failed."; + std::terminate(); + } if (auto *tmp = new (std::nothrow) ApplicationManager1Adaptor{this}; tmp == nullptr) { qCritical() << "new Application Manager Adaptor failed."; @@ -154,7 +157,9 @@ void ApplicationManager1Service::initService(QDBusConnection &connection) noexce watcher->deleteLater(); }; - *sigCon = connect(watcher, &QDBusServiceWatcher::serviceRegistered, singleSlot); + if (watcher != nullptr) { + *sigCon = connect(watcher, &QDBusServiceWatcher::serviceRegistered, singleSlot); + } auto msg = QDBusMessage::createMethodCall("org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", "NameHasOwner"); @@ -192,7 +197,7 @@ void ApplicationManager1Service::addInstanceToApplication(const QString &unitNam m_applicationList.cend(), [&appId](const QSharedPointer &app) { return app->id() == appId; }); - if (appIt == m_applicationList.cend()) [[unlikely]] { + if (appIt == m_applicationList.cend()) { qWarning() << "couldn't find app" << appId << "in application manager."; return; } @@ -201,7 +206,7 @@ void ApplicationManager1Service::addInstanceToApplication(const QString &unitNam const auto &applicationPath = (*appIt)->applicationPath().path(); - if (!(*appIt)->addOneInstance(instanceId, applicationPath, systemdUnitPath.path(), launcher)) [[likely]] { + if (!(*appIt)->addOneInstance(instanceId, applicationPath, systemdUnitPath.path(), launcher)) { qCritical() << "add Instance failed:" << applicationPath << unitName << systemdUnitPath.path(); } } @@ -225,7 +230,7 @@ void ApplicationManager1Service::removeInstanceFromApplication(const QString &un m_applicationList.cend(), [&appId](const QSharedPointer &app) { return app->id() == appId; }); - if (appIt == m_applicationList.cend()) [[unlikely]] { + if (appIt == m_applicationList.cend()) { qWarning() << "couldn't find app" << appId << "in application manager."; return; } @@ -237,12 +242,12 @@ void ApplicationManager1Service::removeInstanceFromApplication(const QString &un return value->property("SystemdUnitPath") == systemdUnitPath; }); - if (instanceIt != appIns.cend()) [[likely]] { + if (instanceIt != appIns.cend()) { (*appIt)->removeOneInstance(instanceIt.key()); return; } - orphanedInstances->removeIf([&systemdUnitPath](const QSharedPointer &ptr) { + orphanedInstances.removeIf([&systemdUnitPath](const QSharedPointer &ptr) { return (ptr->property("SystemdUnitPath").value() == systemdUnitPath); }); } @@ -426,7 +431,7 @@ QHash, QString> ApplicationManager1Service::s } qInfo() << "launch new autostart application " << newApp->id(); - newApp->setAutostartSource({desktopFile.sourcePath()}); + newApp->setAutostartSource({desktopFile.sourcePath(), {}}); ret.insert(newApp, {}); } diff --git a/src/dbus/applicationservice.cpp b/src/dbus/applicationservice.cpp index ae8c690..e97946c 100644 --- a/src/dbus/applicationservice.cpp +++ b/src/dbus/applicationservice.cpp @@ -779,23 +779,29 @@ QList ApplicationService::instances() const noexcept bool ApplicationService::addOneInstance(const QString &instanceId, const QString &application, const QString &systemdUnitPath, - const QString &launcher) + const QString &launcher) noexcept { - auto *service = new InstanceService{instanceId, application, systemdUnitPath, launcher}; - auto *adaptor = new InstanceAdaptor(service); - QString objectPath{m_applicationPath.path() + "/" + instanceId}; - - if (registerObjectToDBus(service, objectPath, InstanceInterface)) { - m_Instances.insert(QDBusObjectPath{objectPath}, QSharedPointer{service}); - service->moveToThread(this->thread()); - adaptor->moveToThread(this->thread()); - emit InterfacesAdded(QDBusObjectPath{objectPath}, getChildInterfacesAndPropertiesFromObject(service)); - return true; + auto *service = new (std::nothrow) InstanceService{instanceId, application, systemdUnitPath, launcher}; + if (service == nullptr) { + qCritical() << "couldn't new InstanceService."; + return false; } - adaptor->deleteLater(); - service->deleteLater(); - return false; + auto *adaptor = new (std::nothrow) InstanceAdaptor{service}; + QString objectPath{m_applicationPath.path() + "/" + instanceId}; + + if (adaptor == nullptr or !registerObjectToDBus(service, objectPath, InstanceInterface)) { + adaptor->deleteLater(); + service->deleteLater(); + return false; + } + + m_Instances.insert(QDBusObjectPath{objectPath}, QSharedPointer{service}); + service->moveToThread(this->thread()); + adaptor->moveToThread(this->thread()); + emit InterfacesAdded(QDBusObjectPath{objectPath}, getChildInterfacesAndPropertiesFromObject(service)); + + return true; } void ApplicationService::removeOneInstance(const QDBusObjectPath &instance) noexcept @@ -817,7 +823,7 @@ void ApplicationService::removeAllInstance() noexcept void ApplicationService::detachAllInstance() noexcept { for (auto &instance : m_Instances.values()) { - orphanedInstances->append(instance); + orphanedInstances.append(instance); instance->setProperty("Orphaned", true); } @@ -869,7 +875,12 @@ std::optional ApplicationService::unescapeExecArgs(const QString &s wordfree(word); delete word; }; + std::unique_ptr words{new (std::nothrow) wordexp_t{0, nullptr, 0}, deleter}; + if (words == nullptr) { + qCritical() << "couldn't new wordexp_t"; + return std::nullopt; + } if (auto ret = wordexp(unescapedStr.toLocal8Bit(), words.get(), WRDE_SHOWERR); ret != 0) { if (ret != 0) { @@ -899,7 +910,7 @@ std::optional ApplicationService::unescapeExecArgs(const QString &s } QStringList execList; - for (int i = 0; i < words->we_wordc; ++i) { + for (std::size_t i = 0; i < words->we_wordc; ++i) { execList.emplace_back(words->we_wordv[i]); } diff --git a/src/dbus/applicationservice.h b/src/dbus/applicationservice.h index a8ad251..2648507 100644 --- a/src/dbus/applicationservice.h +++ b/src/dbus/applicationservice.h @@ -111,7 +111,7 @@ public: bool addOneInstance(const QString &instanceId, const QString &application, const QString &systemdUnitPath, - const QString &launcher); + const QString &launcher) noexcept; void recoverInstances(const QList &instanceList) noexcept; void removeOneInstance(const QDBusObjectPath &instance) noexcept; void removeAllInstance() noexcept; diff --git a/src/dbus/instanceservice.cpp b/src/dbus/instanceservice.cpp index 549d7cc..afec0df 100644 --- a/src/dbus/instanceservice.cpp +++ b/src/dbus/instanceservice.cpp @@ -14,7 +14,10 @@ InstanceService::InstanceService(QString instanceId, QString application, QStrin , m_Application(std::move(application)) , m_SystemdUnitPath(std::move(systemdUnitPath)) { - new PropertiesForwarder{application + "/" + instanceId, this}; + if (auto *tmp = new (std::nothrow) PropertiesForwarder{application + "/" + instanceId, this}; tmp == nullptr) { + qCritical() << "couldn't new PropertiesForwarder for instanceService."; + return; + } } InstanceService::~InstanceService() = default; diff --git a/src/dbus/instanceservice.h b/src/dbus/instanceservice.h index c47ba34..a2773d9 100644 --- a/src/dbus/instanceservice.h +++ b/src/dbus/instanceservice.h @@ -42,6 +42,6 @@ private: QDBusObjectPath m_SystemdUnitPath; }; -Q_GLOBAL_STATIC(QList>, orphanedInstances) +static inline QList> orphanedInstances{}; #endif diff --git a/src/dbus/jobmanager1service.cpp b/src/dbus/jobmanager1service.cpp index d471fba..0b603e4 100644 --- a/src/dbus/jobmanager1service.cpp +++ b/src/dbus/jobmanager1service.cpp @@ -8,10 +8,11 @@ JobManager1Service::JobManager1Service(ApplicationManager1Service *parent) : m_parent(parent) { - new JobManager1Adaptor{this}; - if (!registerObjectToDBus(this, DDEApplicationManager1JobManager1ObjectPath, JobManager1Interface)) { + auto *adaptor = new (std::nothrow) JobManager1Adaptor{this}; + if (adaptor == nullptr or !registerObjectToDBus(this, DDEApplicationManager1JobManager1ObjectPath, JobManager1Interface)) { std::terminate(); } + qRegisterMetaType(); } diff --git a/src/dbus/jobmanager1service.h b/src/dbus/jobmanager1service.h index d972064..5baac34 100644 --- a/src/dbus/jobmanager1service.h +++ b/src/dbus/jobmanager1service.h @@ -58,11 +58,16 @@ public: qOverload(&QVariantList::append), QVariantList{}, QtConcurrent::ReduceOption::OrderedReduce); - QSharedPointer job{new JobService{future}}; + QSharedPointer job{new (std::nothrow) JobService{future}}; + if (job == nullptr) { + qCritical() << "couldn't new JobService."; + future.cancel(); + return {}; + } auto *ptr = job.data(); - new JobAdaptor(ptr); - if (!registerObjectToDBus(ptr, objectPath, JobInterface)) { + auto *adaptor = new (std::nothrow) JobAdaptor(ptr); + if (adaptor == nullptr or !registerObjectToDBus(ptr, objectPath, JobInterface)) { qCritical() << "can't register job to dbus."; future.cancel(); return {}; diff --git a/src/dbus/mimemanager1service.cpp b/src/dbus/mimemanager1service.cpp index 0d18e0c..3c64cd6 100644 --- a/src/dbus/mimemanager1service.cpp +++ b/src/dbus/mimemanager1service.cpp @@ -10,8 +10,8 @@ MimeManager1Service::MimeManager1Service(ApplicationManager1Service *parent) : QObject(parent) { - new MimeManager1Adaptor{this}; - if (!registerObjectToDBus(this, DDEApplicationManager1MimeManager1ObjectPath, MimeManager1Interface)) { + auto *adaptor = new (std::nothrow) MimeManager1Adaptor{this}; + if (adaptor == nullptr or !registerObjectToDBus(this, DDEApplicationManager1MimeManager1ObjectPath, MimeManager1Interface)) { std::terminate(); } } diff --git a/src/dbus/processguesser1service.cpp b/src/dbus/processguesser1service.cpp index f374ad7..2ab84c6 100644 --- a/src/dbus/processguesser1service.cpp +++ b/src/dbus/processguesser1service.cpp @@ -13,11 +13,9 @@ ProcessGuesser1Service::ProcessGuesser1Service(QDBusConnection &connection, Appl qFatal("%s", connection.lastError().message().toLocal8Bit().data()); } - if (auto *tmp = new (std::nothrow) ProcessGuesser1Adaptor{this}; tmp == nullptr) { - qFatal("new Process Guesser Adaptor failed."); - } - - if (!registerObjectToDBus(this, "/org/desktopspec/ProcessGuesser1", "org.desktopspec.ProcessGuesser1")) { + auto *adaptor = new (std::nothrow) ProcessGuesser1Adaptor{this}; + if (adaptor == nullptr or + !registerObjectToDBus(this, "/org/desktopspec/ProcessGuesser1", "org.desktopspec.ProcessGuesser1")) { std::terminate(); } } diff --git a/src/desktopfileparser.cpp b/src/desktopfileparser.cpp index 3297e40..f1a1cd8 100644 --- a/src/desktopfileparser.cpp +++ b/src/desktopfileparser.cpp @@ -165,7 +165,7 @@ ParserError DesktopFileParser::addEntry(typename Groups::iterator &group) noexce } auto localeMap = keyIt->value(); - if (auto valueIt = localeMap.find(localeStr) != localeMap.end()) { + if (localeMap.find(localeStr) != localeMap.end()) { qWarning() << "duplicate locale key:" << key << "skip."; return ParserError::NoError; } diff --git a/src/launchoptions.cpp b/src/launchoptions.cpp index 04371c9..661cc22 100644 --- a/src/launchoptions.cpp +++ b/src/launchoptions.cpp @@ -86,7 +86,6 @@ QStringList setUserLaunchOption::generateCommandLine() const noexcept QString userName = destUser->pw_name; ret.append(userName); - struct passwd *curUser = getpwuid(curUID); ret.append("env"); ret.append("DISPLAY=:0");