From 3cf2441de5737445725c5acdd591872389861bca Mon Sep 17 00:00:00 2001 From: Eskil Abrahamsen Blomfeldt Date: Thu, 08 Oct 2026 13:18:24 +0200 Subject: [PATCH] Fix buffer size mismatch in rare fallback path in Qt Quick Shapes/GeometryRenderer For index buffers with odd number counts and in the case where qTriangulate() returns 16-bit indexes, we would truncate the number, so the memcpy() into the buffer would write past the buffer by one short. The code path is only reachable on systems where 32-bit indexes are unsupported, such as GLES2 without extension GL_OES_element_index_uint. On these systems, static shapes will fail to render at all because we always create 32-bit index buffers and only make them 16-bit when updating (separate bug). So in practice, Qt Quick Shapes is not really usable on such platforms. Fixes: QTBUG-151299 Change-Id: I242d417c289b57bb3b00901c8389594dce6ab57a Reviewed-by: Laszlo Agocs (cherry picked from commit 676423dd3baa884ae3acb457e1135f180b652bc8) Reviewed-by: Qt Cherry-pick Bot (cherry picked from commit e1d8923eb28e33b42cb68ce005a055573f8b7592) Reviewed-by: Jani Heikkinen --- diff --git a/src/quickshapes/qquickshapegenericrenderer.cpp b/src/quickshapes/qquickshapegenericrenderer.cpp index 669d447..644476c 100644 --- a/src/quickshapes/qquickshapegenericrenderer.cpp +++ b/src/quickshapes/qquickshapegenericrenderer.cpp @@ -478,14 +478,18 @@ if (ts.indices.type() == QVertexIndexVector::UnsignedShort) { *indexType = QSGGeometry::UnsignedShortType; // fillIndices is still QList. Just resize to N/2 and pack - // the N quint16s into it. - fillIndices->resize(ts.indices.size() / 2); + // the N quint16s into it. To ensure that there is enough space to hold the full buffer, + // we round the number up. Any redundant padding indexes will be ignored as the renderer + // processes geometry in triplets. + fillIndices->resize((ts.indices.size() + 1) / 2); indexByteSize = ts.indices.size() * sizeof(quint16); } else { *indexType = QSGGeometry::UnsignedIntType; fillIndices->resize(ts.indices.size()); indexByteSize = ts.indices.size() * sizeof(quint32); } + + Q_ASSERT(fillIndices->size() * sizeof(quint32) >= indexByteSize); memcpy(fillIndices->data(), ts.indices.data(), indexByteSize); } diff --git a/tests/auto/quickvectorimage/qquickvectorimage/data/svg/indexbufferoverflow.svg b/tests/auto/quickvectorimage/qquickvectorimage/data/svg/indexbufferoverflow.svg new file mode 100644 index 0000000..78a8c92 --- /dev/null +++ b/tests/auto/quickvectorimage/qquickvectorimage/data/svg/indexbufferoverflow.svg @@ -0,0 +1,7 @@ + + + + + diff --git a/tests/auto/quickvectorimage/qquickvectorimage/tst_qquickvectorimage.cpp b/tests/auto/quickvectorimage/qquickvectorimage/tst_qquickvectorimage.cpp index 82f3bce..9ae5e89 100644 --- a/tests/auto/quickvectorimage/qquickvectorimage/tst_qquickvectorimage.cpp +++ b/tests/auto/quickvectorimage/qquickvectorimage/tst_qquickvectorimage.cpp @@ -29,6 +29,7 @@ void asyncShapes_data(); void asyncShapes(); void clearSource(); + void renderAnimatedFrames(); }; tst_QQuickVectorImage::tst_QQuickVectorImage() @@ -147,6 +148,31 @@ QVERIFY(d_ptr->rootItem == nullptr); } +void tst_QQuickVectorImage::renderAnimatedFrames() +{ + QQmlEngine engine; + engine.rootContext()->setContextProperty(QStringLiteral("fileName"), + testFileUrl("svg/indexbufferoverflow.svg")); + + QQuickWindow window; + window.resize(512, 512); + window.create(); + + QQmlComponent c(&engine, testFileUrl("vectorimage.qml")); + QVERIFY2(c.isReady(), qPrintable(c.errorString())); + + std::unique_ptr object(c.create()); + QQuickVectorImage *image = qobject_cast(object.get()); + QVERIFY(image != nullptr); + + image->setParentItem(window.contentItem()); + + for (int i = 0; i < 150; ++i) { + window.grabWindow(); + QTest::qWait(16); + } +} + QTEST_MAIN(tst_QQuickVectorImage) #include "tst_qquickvectorimage.moc"