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
This commit is contained in:
Vlad Zahorodnii 2024-02-02 12:03:33 +02:00
parent 92f4a95bb5
commit e58451fc01
4 changed files with 32 additions and 15 deletions

View file

@ -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

View file

@ -5,6 +5,7 @@
*/
#include "plasmavirtualdesktop.h"
#include "display.h"
#include "wayland/quirks.h"
#include <QDebug>
#include <QTimer>
@ -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));
}
}

View file

@ -9,6 +9,7 @@
#include "plasmavirtualdesktop.h"
#include "surface.h"
#include "utils/common.h"
#include "wayland/quirks.h"
#include <QFile>
#include <QHash>
@ -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:

27
src/wayland/quirks.h Normal file
View file

@ -0,0 +1,27 @@
/*
SPDX-FileCopyrightText: 2024 David Edmundson <kde@davidedmundson.co.uk>
SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
#pragma once
#include <QString>
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