Skip to content

Commit

Permalink
Fix UB found by libASAN build
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkewley committed Jul 29, 2024
1 parent 6bd26d9 commit 626bf0f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/oscar/Graphics/GraphicsImplementation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4923,8 +4923,14 @@ class osc::Mesh::Impl final {
{
indices_are_32bit_ = false;
num_indices_ = indices.size();
indices_data_.resize((indices.size()+1)/2);
rgs::copy(indices, &indices_data_.front().u16.a);

if (not indices.empty()) {
indices_data_.resize((indices.size()+1)/2);
rgs::copy(indices, &indices_data_.front().u16.a);
}
else {
indices_data_.clear();
}

range_check_indices_and_recalculate_bounds(flags);
version_->reset();
Expand Down
18 changes: 18 additions & 0 deletions tests/testoscar/Graphics/TestMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,24 @@ TEST(Mesh, SetIndicesThrowsIfOutOfBounds)
ASSERT_ANY_THROW({ m.set_indices(iota_index_range(3, 6)); }) << "should throw: indices are out-of-bounds";
}

TEST(Mesh, SetIndices16BitWorksWithEmptyVector)
{
std::vector<uint16_t> indices;
Mesh m;
m.set_vertices(generate_vertices(3));
m.set_indices(indices); // should just work
ASSERT_TRUE(m.indices().empty());
}

TEST(Mesh, SetIndices32BitWorksWithEmptyVector)
{
std::vector<uint32_t> indices;
Mesh m;
m.set_vertices(generate_vertices(3));
m.set_indices(indices); // should just work
ASSERT_TRUE(m.indices().empty());
}

TEST(Mesh, SetIndiciesWithDontValidateIndicesAndDontRecalculateBounds)
{
Mesh m;
Expand Down

0 comments on commit 626bf0f

Please sign in to comment.