Client: Don't crash with long window titles using UTF-16 characters
Previously, we set the max length in QString character length, which means UTF-16 characters (of potentially three bytes) counts as one character. The max limit of libwayland, however, is in bytes (and the string itself is converted to UTF-8). Fix it by dividing the character limit by three because in the worst case each UTF-16 character will use three bytes when UTF-8 encoded. Fixes: QTBUG-78478 Change-Id: Idf4721894e0fe6f3cd92bdc6ada7b0ea4199ea63 Reviewed-by: Paul Olav Tvete <paul.tvete@qt.io>
This commit is contained in:
parent
7dfa3f9d50
commit
7c4b2334a3
@ -298,8 +298,10 @@ void QWaylandWindow::setWindowTitle(const QString &title)
|
|||||||
const QString formatted = formatWindowTitle(title, separator);
|
const QString formatted = formatWindowTitle(title, separator);
|
||||||
|
|
||||||
const int libwaylandMaxBufferSize = 4096;
|
const int libwaylandMaxBufferSize = 4096;
|
||||||
// Some parts of the buffer is used for metadata, so subtract 100 to be on the safe side
|
// Some parts of the buffer is used for metadata, so subtract 100 to be on the safe side.
|
||||||
const int maxLength = libwaylandMaxBufferSize - 100;
|
// 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;
|
||||||
|
|
||||||
auto truncated = QStringRef(&formatted).left(maxLength);
|
auto truncated = QStringRef(&formatted).left(maxLength);
|
||||||
if (truncated.length() < formatted.length()) {
|
if (truncated.length() < formatted.length()) {
|
||||||
|
@ -178,6 +178,7 @@ private slots:
|
|||||||
void hiddenPopupParent();
|
void hiddenPopupParent();
|
||||||
void glWindow();
|
void glWindow();
|
||||||
void longWindowTitle();
|
void longWindowTitle();
|
||||||
|
void longWindowTitleWithUtf16Characters();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
MockCompositor *compositor = nullptr;
|
MockCompositor *compositor = nullptr;
|
||||||
@ -494,6 +495,16 @@ void tst_WaylandClient::longWindowTitle()
|
|||||||
QTRY_VERIFY(compositor->surface());
|
QTRY_VERIFY(compositor->surface());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void tst_WaylandClient::longWindowTitleWithUtf16Characters()
|
||||||
|
{
|
||||||
|
QWindow window;
|
||||||
|
QString absurdlyLongTitle = QString("三").repeated(10000);
|
||||||
|
Q_ASSERT(absurdlyLongTitle.length() == 10000); // just making sure the test isn't broken
|
||||||
|
window.setTitle(absurdlyLongTitle);
|
||||||
|
window.show();
|
||||||
|
QTRY_VERIFY(compositor->surface());
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
setenv("XDG_RUNTIME_DIR", ".", 1);
|
setenv("XDG_RUNTIME_DIR", ".", 1);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user