From 06f7ecb5ce6899d8fccf6f3ce011210d91ebb673 Mon Sep 17 00:00:00 2001 From: uwezkhan <114483941+uwezkhan@users.noreply.github.com> Date: Tue, 24 Nov 2026 19:11:50 +0530 Subject: [PATCH] =?UTF-8?q?ptcloud:=20bound=20triangle=20vertex=20indices?= =?UTF-8?q?=20in=20triangleRasterize=20=F0=9F=A4=96=F0=9F=A4=96?= =?UTF-8?q?=F0=9F=A4=96=20(#30640)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ptcloud: bound triangle vertex indices in triangleRasterize * ptcloud: use checkRange for the triangle index check --- modules/ptcloud/src/rendering.cpp | 11 +++++-- modules/ptcloud/test/test_rendering.cpp | 43 +++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/modules/ptcloud/src/rendering.cpp b/modules/ptcloud/src/rendering.cpp index ca0b9bbdf9..f2ab900a7a 100644 --- a/modules/ptcloud/src/rendering.cpp +++ b/modules/ptcloud/src/rendering.cpp @@ -229,6 +229,15 @@ static void triangleRasterizeInternal(InputArray _vertices, InputArray _indices, triangles = triangles.reshape(3, 1).t(); nTriangles = (int)triangles.total(); + // indices are used to read vertices and colors, check them all before anything is drawn + Mat flatIndices = triangles.reshape(1); + Point badPos; + if (!checkRange(flatIndices, true, &badPos, 0, nVerts)) + { + CV_Error(Error::StsOutOfRange, cv::format("Triangle %d has vertex index %d, expected [0, %d)", + badPos.y, flatIndices.at(badPos), nVerts)); + } + if (!_colors.empty()) { int colorType = _colors.type(); @@ -308,8 +317,6 @@ static void triangleRasterizeInternal(InputArray _vertices, InputArray _indices, for (int i = 0; i < 3; i++) { int idx = tri[i]; - CV_DbgAssert(idx >= 0 && idx < nVerts); - col[i] = colors.empty() ? Vec3f::all(0) : colors.at(idx); ver[i] = screenVertices.at(idx); } diff --git a/modules/ptcloud/test/test_rendering.cpp b/modules/ptcloud/test/test_rendering.cpp index 4df888c5c2..0a4bb6f737 100644 --- a/modules/ptcloud/test/test_rendering.cpp +++ b/modules/ptcloud/test/test_rendering.cpp @@ -942,5 +942,48 @@ INSTANTIATE_TEST_CASE_P(Rendering, RenderingTest, ::testing::Values( RenderTestParamType { std::make_tuple(700, 700), RASTERIZE_SHADING_WHITE, RASTERIZE_CULLING_NONE, ModelType::File, CV_32F, CV_32S } )); + +TEST(Rendering, outOfRangeIndices) +{ + std::vector verts = { {0.f, 0.f, -1.f}, {1.f, 0.f, -1.f}, {0.f, 1.f, -1.f} }; + std::vector colors = { {1.f, 0.f, 0.f}, {0.f, 1.f, 0.f}, {0.f, 0.f, 1.f} }; + Matx44f cameraPose = Matx44f::eye(); + double fovY = CV_PI / 2, zNear = 0.1, zFar = 10.0; + TriangleRasterizeSettings settings; + settings.setCullingMode(RASTERIZE_CULLING_NONE); + + { + std::vector indices = { {0, 1, 2} }; + Mat colorBuf(32, 32, CV_32FC3, Scalar::all(0)), depthBuf(32, 32, CV_32FC1, Scalar(zFar)); + triangleRasterize(verts, indices, colors, colorBuf, depthBuf, cameraPose, fovY, zNear, zFar, settings); + EXPECT_GT(countNonZero(depthBuf < zFar), 0); + } + + const std::vector badTriangles = { {0, 1, 3}, {0, -1, 2}, {1000, 1, 2} }; + for (const Vec3i& badTri : badTriangles) + { + // the valid first triangle should not be drawn either + std::vector indices = { {0, 1, 2}, badTri }; + Mat colorBuf(32, 32, CV_32FC3, Scalar::all(0)), depthBuf(32, 32, CV_32FC1, Scalar(zFar)); + + int code = 0; + try { triangleRasterize(verts, indices, colors, colorBuf, depthBuf, cameraPose, fovY, zNear, zFar, settings); } + catch (const cv::Exception& e) { code = e.code; } + EXPECT_EQ(Error::StsOutOfRange, code) << badTri; + EXPECT_EQ(0, countNonZero(colorBuf.reshape(1))) << badTri; + EXPECT_EQ(0, countNonZero(depthBuf != zFar)) << badTri; + + code = 0; + try { triangleRasterizeColor(verts, indices, colors, colorBuf, cameraPose, fovY, zNear, zFar, settings); } + catch (const cv::Exception& e) { code = e.code; } + EXPECT_EQ(Error::StsOutOfRange, code) << badTri; + + code = 0; + try { triangleRasterizeDepth(verts, indices, depthBuf, cameraPose, fovY, zNear, zFar, settings); } + catch (const cv::Exception& e) { code = e.code; } + EXPECT_EQ(Error::StsOutOfRange, code) << badTri; + } +} + } // namespace :: } // namespace opencv_test