From e1d8923eb28e33b42cb68ce005a055573f8b7592 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 Pick-to: 6.8 Change-Id: I242d417c289b57bb3b00901c8389594dce6ab57a Reviewed-by: Laszlo Agocs (cherry picked from commit 676423dd3baa884ae3acb457e1135f180b652bc8) Reviewed-by: Qt Cherry-pick Bot --- diff --git a/src/quickshapes/qquickshapegenericrenderer.cpp b/src/quickshapes/qquickshapegenericrenderer.cpp index 8c90565..5214f6d 100644 --- a/src/quickshapes/qquickshapegenericrenderer.cpp +++ b/src/quickshapes/qquickshapegenericrenderer.cpp @@ -495,14 +495,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 36979f5..8ee373a 100644 --- a/tests/auto/quickvectorimage/qquickvectorimage/tst_qquickvectorimage.cpp +++ b/tests/auto/quickvectorimage/qquickvectorimage/tst_qquickvectorimage.cpp @@ -33,6 +33,7 @@ void clearSource(); void renderFiles_data(); void renderFiles(); + void renderAnimatedFrames(); void sourceData(); }; @@ -197,6 +198,31 @@ window.grabWindow(); } +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); + } +} + void tst_QQuickVectorImage::sourceData() { QFile file(QStringLiteral(":/svgs/gradientxform.svg"));