From cdf8ad2c944c1a1196465e0a79a6bc8a43a5a7d1 Mon Sep 17 00:00:00 2001 From: Trial97 Date: Wed, 25 Jun 2025 13:13:42 +0300 Subject: [PATCH] fix: escape quetes in shorcut creation arguments Signed-off-by: Trial97 --- launcher/FileSystem.cpp | 48 +++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/launcher/FileSystem.cpp b/launcher/FileSystem.cpp index 5136e7954..308f8620e 100644 --- a/launcher/FileSystem.cpp +++ b/launcher/FileSystem.cpp @@ -897,6 +897,29 @@ QString getApplicationsDir() return QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation); } +QString quoteArgs(const QStringList& args, const QString& wrap, const QString& escapeChar, bool wrapOnlyIfNeeded = false) +{ + QString result; + + auto size = args.size(); + for (int i = 0; i < size; ++i) { + QString arg = args[i]; + arg.replace(wrap, escapeChar); + + bool needsWrapping = !wrapOnlyIfNeeded || arg.contains(' ') || arg.contains('\t') || arg.contains(wrap); + + if (needsWrapping) + result += wrap + arg + wrap; + else + result += arg; + + if (i < size - 1) + result += ' '; + } + + return result; +} + // Cross-platform Shortcut creation QString createShortcut(QString destination, QString target, QStringList args, QString name, QString icon) { @@ -940,9 +963,7 @@ QString createShortcut(QString destination, QString target, QStringList args, QS f.open(QIODevice::WriteOnly | QIODevice::Text); QTextStream stream(&f); - QString argstring; - if (!args.empty()) - argstring = " \"" + args.join("\" \"") + "\""; + auto argstring = quoteArgs(args, "\"", "\\\""); stream << "#!/bin/bash" << "\n"; stream << "\"" << target << "\" " << argstring << "\n"; @@ -984,14 +1005,12 @@ QString createShortcut(QString destination, QString target, QStringList args, QS f.open(QIODevice::WriteOnly | QIODevice::Text); QTextStream stream(&f); - QString argstring; - if (!args.empty()) - argstring = " '" + args.join("' '") + "'"; + auto argstring = quoteArgs(args, "'", "'\\''"); stream << "[Desktop Entry]" << "\n"; stream << "Type=Application" << "\n"; stream << "Categories=Game;ActionGame;AdventureGame;Simulation" << "\n"; - stream << "Exec=\"" << target.toLocal8Bit() << "\"" << argstring.toLocal8Bit() << "\n"; + stream << "Exec=\"" << target.toLocal8Bit() << "\" " << argstring.toLocal8Bit() << "\n"; stream << "Name=" << name.toLocal8Bit() << "\n"; if (!icon.isEmpty()) { stream << "Icon=" << icon.toLocal8Bit() << "\n"; @@ -1030,20 +1049,7 @@ QString createShortcut(QString destination, QString target, QStringList args, QS return QString(); } - QString argStr; - int argCount = args.count(); - for (int i = 0; i < argCount; i++) { - if (args[i].contains(' ')) { - argStr.append('"').append(args[i]).append('"'); - } else { - argStr.append(args[i]); - } - - if (i < argCount - 1) { - argStr.append(" "); - } - } - + auto argStr = quoteArgs(args, "\"", "\\\"", true); if (argStr.length() >= MAX_PATH) { qWarning() << "Arguments string is too long!"; return QString();