From 6616e014c51e829786f3e34b431d178a8823d388 Mon Sep 17 00:00:00 2001
From: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@qt.io>
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 <laszlo.agocs@qt.io>
(cherry picked from commit 676423dd3baa884ae3acb457e1135f180b652bc8)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit e1d8923eb28e33b42cb68ce005a055573f8b7592)
---

diff --git a/src/quickshapes/qquickshapegenericrenderer.cpp b/src/quickshapes/qquickshapegenericrenderer.cpp
index 556f034..abf6696 100644
--- a/src/quickshapes/qquickshapegenericrenderer.cpp
+++ b/src/quickshapes/qquickshapegenericrenderer.cpp
@@ -472,15 +472,19 @@
     size_t indexByteSize;
     if (ts.indices.type() == QVertexIndexVector::UnsignedShort) {
         *indexType = QSGGeometry::UnsignedShortType;
-        // fillIndices is still QVector<quint32>. Just resize to N/2 and pack
-        // the N quint16s into it.
-        fillIndices->resize(ts.indices.size() / 2);
+        // fillIndices is still QList<quint32>. Just resize to N/2 and pack
+        // 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);
 }
 
