Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added missing bounds check to TriangleMesh constructor #5121

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion xs/src/libslic3r/TriangleMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ TriangleMesh::TriangleMesh()
stl_initialize(&this->stl);
}

TriangleMesh::TriangleMesh(const Pointf3* points, const Point3* facets, size_t n_facets)
TriangleMesh::TriangleMesh(const Pointf3* points, size_t n_points, const Point3* facets, size_t n_facets)
: repaired(false)
{
stl_initialize(&this->stl);
Expand All @@ -51,6 +51,13 @@ TriangleMesh::TriangleMesh(const Pointf3* points, const Point3* facets, size_t n
stl_allocate(&stl);

for (int i = 0; i < stl.stats.number_of_facets; i++) {

if (facets[i].x >= n_points ||
facets[i].y >= n_points ||
facets[i].z >= n_points) {
throw std::runtime_error("Invalid facet");
}

stl_facet facet;
facet.normal.x = 0;
facet.normal.y = 0;
Expand Down
4 changes: 2 additions & 2 deletions xs/src/libslic3r/TriangleMesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class TriangleMesh
/// First argument is a container (either vector or array) of Pointf3 for the vertex data.
/// Second argument is container of facets (currently Point3).
template <typename Vertex_Cont, typename Facet_Cont>
TriangleMesh(const Vertex_Cont& vertices, const Facet_Cont& facets) : TriangleMesh(vertices.data(), facets.data(), facets.size()) {}
TriangleMesh(const Vertex_Cont& vertices, const Facet_Cont& facets) : TriangleMesh(vertices.data(), vertices.size(), facets.data(), facets.size()) {}

TriangleMesh(const TriangleMesh &other);
/// copy assignment
Expand Down Expand Up @@ -163,7 +163,7 @@ class TriangleMesh
/// Private constructor that is called from the public sphere.
/// It doesn't do any bounds checking on points and operates on raw pointers, so we hide it.
/// Other constructors can call this one!
TriangleMesh(const Pointf3* points, const Point3* facets, size_t n_facets);
TriangleMesh(const Pointf3* points, size_t n_points, const Point3* facets, size_t n_facets);

/// Perform the mechanics of a stl copy
void clone(const TriangleMesh& other);
Expand Down