From 9d3e7e9c2edb38a6bca9bb6c1cdef02c0a329ec4 Mon Sep 17 00:00:00 2001
From: Alex Blasche <alexander.blasche@theqtcompany.com>
Date: Wed, 23 Dec 2015 13:32:05 +0100
Subject: [PATCH] Bluez5: Sanitize app name to be valid DBus object path name

This may happen when the app name contains for example a dash ('-').

Task-number: QTBUG-49402
Change-Id: I04b289b0723e2979a67c93e335205556bf1eb30e
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@theqtcompany.com>
---
 src/bluetooth/bluez/bluez5_helper.cpp         | 22 +++++++++++++++++++
 src/bluetooth/bluez/bluez5_helper_p.h         |  2 ++
 src/bluetooth/qbluetoothserviceinfo_bluez.cpp |  2 +-
 .../qbluetoothtransferreply_bluez.cpp         |  2 +-
 4 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/src/bluetooth/bluez/bluez5_helper.cpp b/src/bluetooth/bluez/bluez5_helper.cpp
index 384b9979..14e064e1 100644
--- a/src/bluetooth/bluez/bluez5_helper.cpp
+++ b/src/bluetooth/bluez/bluez5_helper.cpp
@@ -339,4 +339,26 @@ QString findAdapterForAddress(const QBluetoothAddress &wantedAddress, bool *ok =
     return QString(); // nothing matching found
 }
 
+/*
+    Removes every character that cannot be used in QDbusObjectPath
+
+    See QDbusUtil::isValidObjectPath(QString) for more details.
+ */
+QString sanitizeNameForDBus(const QString &text)
+{
+    QString appName = text;
+    for (int i = 0; i < appName.length(); i++) {
+        ushort us = appName[i].unicode();
+        bool valid = (us >= 'a' && us <= 'z')
+                      || (us >= 'A' && us <= 'Z')
+                      || (us >= '0' && us <= '9')
+                      || (us == '_');
+
+        if (!valid)
+            appName[i] = QLatin1Char('_');
+    }
+
+    return appName;
+}
+
 QT_END_NAMESPACE
diff --git a/src/bluetooth/bluez/bluez5_helper_p.h b/src/bluetooth/bluez/bluez5_helper_p.h
index a3f164b0..019fe635 100644
--- a/src/bluetooth/bluez/bluez5_helper_p.h
+++ b/src/bluetooth/bluez/bluez5_helper_p.h
@@ -59,6 +59,8 @@ QT_BEGIN_NAMESPACE
 
 bool isBluez5();
 
+QString sanitizeNameForDBus(const QString& text);
+
 QString findAdapterForAddress(const QBluetoothAddress &wantedAddress, bool *ok);
 
 class QtBluezDiscoveryManagerPrivate;
diff --git a/src/bluetooth/qbluetoothserviceinfo_bluez.cpp b/src/bluetooth/qbluetoothserviceinfo_bluez.cpp
index e3fa81d1..d18f4bb1 100644
--- a/src/bluetooth/qbluetoothserviceinfo_bluez.cpp
+++ b/src/bluetooth/qbluetoothserviceinfo_bluez.cpp
@@ -294,7 +294,7 @@ bool QBluetoothServiceInfoPrivate::registerService(const QBluetoothAddress &loca
         // create path
         profilePath = profilePathTemplate;
         profilePath.append(QString::fromLatin1("/%1%2/%3").
-                           arg(QCoreApplication::applicationName()).
+                           arg(sanitizeNameForDBus(QCoreApplication::applicationName())).
                            arg(QCoreApplication::applicationPid()).
                            arg(pathCounter.fetchAndAddOrdered(1)));
 
diff --git a/src/bluetooth/qbluetoothtransferreply_bluez.cpp b/src/bluetooth/qbluetoothtransferreply_bluez.cpp
index f7f72873..3362b2c1 100644
--- a/src/bluetooth/qbluetoothtransferreply_bluez.cpp
+++ b/src/bluetooth/qbluetoothtransferreply_bluez.cpp
@@ -91,7 +91,7 @@ QBluetoothTransferReplyBluez::QBluetoothTransferReplyBluez(QIODevice *input, con
 
         m_agent_path = agentPath;
         m_agent_path.append(QStringLiteral("/%1%2/%3").
-                            arg(QCoreApplication::applicationName()).
+                            arg(sanitizeNameForDBus(QCoreApplication::applicationName())).
                             arg(QCoreApplication::applicationPid()).
                             arg(agentPathCounter.fetchAndAddOrdered(1)));
 
-- 
GitLab