Skip to content

Commit

Permalink
[MeL/IO] Explicit checked material ids conversion
Browse files Browse the repository at this point in the history
The legacy reader is designed for unsigned type of material ids,
but current implementation works on signed integers.

This fixes a compilation warning.
  • Loading branch information
endJunction committed Aug 24, 2024
1 parent e0ae060 commit 8db8708
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions MeshLib/IO/Legacy/MeshIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,20 @@

namespace
{
std::size_t readMaterialID(std::istream& in)
int readMaterialID(std::istream& in)
{
unsigned index;
unsigned material_id;
if (!(in >> index >> material_id))

if (!(in >> index >> material_id) ||
material_id > static_cast<unsigned>(std::numeric_limits<int>::max()))
{
return std::numeric_limits<std::size_t>::max();
// If read incorrectly or the material_id is not safely convertible to
// int.
return std::numeric_limits<int>::max();
}
return material_id;
// Safe conversion was checked above.
return static_cast<int>(material_id);
}

MeshLib::Element* readElement(std::istream& in,
Expand Down Expand Up @@ -289,7 +294,7 @@ MeshLib::Mesh* MeshIO::loadMeshFromFile(const std::string& file_name)
{
std::vector<MeshLib::Node*> nodes;
std::vector<MeshLib::Element*> elements;
std::vector<std::size_t> materials;
std::vector<int> materials;

while (!in.eof())
{
Expand Down

0 comments on commit 8db8708

Please sign in to comment.