Skip to content

Commit

Permalink
fix: crashed when access DSGApplication::id early
Browse files Browse the repository at this point in the history
Add fallback, using fileName or cmdline as AppId.
Remove assert for empty appId.
  • Loading branch information
18202781743 committed May 17, 2024
1 parent 653f6dc commit db758e9
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/dsgapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <sys/syscall.h>
#include <unistd.h>

#include <QFile>
#include <QByteArray>
#include <QCoreApplication>
#include <QDebug>
Expand Down Expand Up @@ -47,6 +48,15 @@ static bool isServiceActivatable(const QString &service)
return activatableNames.value().contains(service);
}

// Format appId to valid.
static QByteArray formatAppId(const QByteArray &appId)
{
static const QRegularExpression regex("[^\\w\\-\\.]");
QString format(QString::fromStdString(appId.toStdString()));
format = format.replace(regex, QStringLiteral("."));
return format.toLocal8Bit();
}

QByteArray DSGApplication::id()
{
static QByteArray selfId = getSelfAppId();
Expand All @@ -55,11 +65,23 @@ QByteArray DSGApplication::id()
QByteArray result = selfId;
if (!qEnvironmentVariableIsSet("DTK_DISABLED_FALLBACK_APPID")) {
result = QCoreApplication::applicationName().toLocal8Bit();
if (result.isEmpty()) {
QFile file("/proc/self/cmdline");
if (file.open(QIODevice::ReadOnly))
result = file.readLine();
}
if (result.isEmpty()) {
const QFile file(QFile::symLinkTarget("/proc/self/exe"));
if (file.exists())
result = file.fileName().toLocal8Bit();
}
if (!result.isEmpty()) {
result = formatAppId(result);
qCDebug(dsgApp) << "The applicatiion ID is fallback to " << result;
}
}
Q_ASSERT(!result.isEmpty());
if (result.isEmpty()) {
qt_assert("The application ID is empty", __FILE__, __LINE__);
}
if (result.isEmpty())
qCWarning(dsgApp) << "The application ID is empty.";

return result;
}
Expand Down

0 comments on commit db758e9

Please sign in to comment.