From 8db8708d75fc33dec43be20d28298304d91f4963 Mon Sep 17 00:00:00 2001 From: Dmitri Naumov Date: Thu, 22 Aug 2024 15:11:21 +0200 Subject: [PATCH] [MeL/IO] Explicit checked material ids conversion The legacy reader is designed for unsigned type of material ids, but current implementation works on signed integers. This fixes a compilation warning. --- MeshLib/IO/Legacy/MeshIO.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/MeshLib/IO/Legacy/MeshIO.cpp b/MeshLib/IO/Legacy/MeshIO.cpp index faf2e8b7f3d..898d2741049 100644 --- a/MeshLib/IO/Legacy/MeshIO.cpp +++ b/MeshLib/IO/Legacy/MeshIO.cpp @@ -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(std::numeric_limits::max())) { - return std::numeric_limits::max(); + // If read incorrectly or the material_id is not safely convertible to + // int. + return std::numeric_limits::max(); } - return material_id; + // Safe conversion was checked above. + return static_cast(material_id); } MeshLib::Element* readElement(std::istream& in, @@ -289,7 +294,7 @@ MeshLib::Mesh* MeshIO::loadMeshFromFile(const std::string& file_name) { std::vector nodes; std::vector elements; - std::vector materials; + std::vector materials; while (!in.eof()) {