From e58451fc01e4a7713a33474a7243f7489ace24a4 Mon Sep 17 00:00:00 2001 From: Vlad Zahorodnii Date: Fri, 2 Feb 2024 12:03:33 +0200 Subject: [PATCH] wayland: Truncate virtual desktop names Virtual desktop names are user defined strings so they can exceed the maximum size of a wayland message size. BUG: 480614 --- src/wayland/CMakeLists.txt | 1 + src/wayland/plasmavirtualdesktop.cpp | 5 +++-- src/wayland/plasmawindowmanagement.cpp | 14 +------------ src/wayland/quirks.h | 27 ++++++++++++++++++++++++++ 4 files changed, 32 insertions(+), 15 deletions(-) create mode 100644 src/wayland/quirks.h diff --git a/src/wayland/CMakeLists.txt b/src/wayland/CMakeLists.txt index eea3fb174d..7d9d7b0905 100644 --- a/src/wayland/CMakeLists.txt +++ b/src/wayland/CMakeLists.txt @@ -359,6 +359,7 @@ install(FILES primaryselectiondevicemanager_v1.h primaryselectionoffer_v1.h primaryselectionsource_v1.h + quirks.h relativepointer_v1.h screencast_v1.h screenedge_v1.h diff --git a/src/wayland/plasmavirtualdesktop.cpp b/src/wayland/plasmavirtualdesktop.cpp index 1967f34a8e..c2a2ed088d 100644 --- a/src/wayland/plasmavirtualdesktop.cpp +++ b/src/wayland/plasmavirtualdesktop.cpp @@ -5,6 +5,7 @@ */ #include "plasmavirtualdesktop.h" #include "display.h" +#include "wayland/quirks.h" #include #include @@ -240,7 +241,7 @@ void PlasmaVirtualDesktopInterfacePrivate::org_kde_plasma_virtual_desktop_bind_r send_desktop_id(resource->handle, id); if (!name.isEmpty()) { - send_name(resource->handle, name); + send_name(resource->handle, truncate(name)); } if (active) { @@ -272,7 +273,7 @@ void PlasmaVirtualDesktopInterface::setName(const QString &name) const auto clientResources = d->resourceMap(); for (auto resource : clientResources) { - d->send_name(resource->handle, name); + d->send_name(resource->handle, truncate(name)); } } diff --git a/src/wayland/plasmawindowmanagement.cpp b/src/wayland/plasmawindowmanagement.cpp index b46c80c0ae..ece82e8dd5 100644 --- a/src/wayland/plasmawindowmanagement.cpp +++ b/src/wayland/plasmawindowmanagement.cpp @@ -9,6 +9,7 @@ #include "plasmavirtualdesktop.h" #include "surface.h" #include "utils/common.h" +#include "wayland/quirks.h" #include #include @@ -26,19 +27,6 @@ namespace KWin static const quint32 s_version = 16; static const quint32 s_activationVersion = 1; -// any strings that come from user-defined sources -// that could exceed the maximum wayland length (i.e xwayland clients) -// need to be truncated to the maximumWaylandBufferSize -static QString truncate(const QString &stringIn) -{ - const int libwaylandMaxBufferSize = 4096; - // Some parts of the buffer is used for metadata, so subtract 100 to be on the safe side. - // Also, QString is in utf-16, which means that in the worst case each character will be - // three bytes when converted to utf-8 (which is what libwayland uses), so divide by three. - const int maxLength = libwaylandMaxBufferSize / 3 - 100; - return stringIn.left(maxLength); -} - class PlasmaWindowManagementInterfacePrivate : public QtWaylandServer::org_kde_plasma_window_management { public: diff --git a/src/wayland/quirks.h b/src/wayland/quirks.h new file mode 100644 index 0000000000..fe69df70ee --- /dev/null +++ b/src/wayland/quirks.h @@ -0,0 +1,27 @@ +/* + SPDX-FileCopyrightText: 2024 David Edmundson + + SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL +*/ +#pragma once + +#include + +namespace KWin +{ + +/** + * Any strings that come from user-defined sources that could exceed the maximum wayland length (e.g. xwayland clients) + * need to be truncated to the maximumWaylandBufferSize. + */ +static inline QString truncate(const QString &stringIn) +{ + const int libwaylandMaxBufferSize = 4096; + // Some parts of the buffer is used for metadata, so subtract 100 to be on the safe side. + // Also, QString is in utf-16, which means that in the worst case each character will be + // three bytes when converted to utf-8 (which is what libwayland uses), so divide by three. + const int maxLength = libwaylandMaxBufferSize / 3 - 100; + return stringIn.left(maxLength); +} + +} // namespace KWin