diff --git a/tiny_obj_loader.h b/tiny_obj_loader.h index 025f112..d52e97c 100644 --- a/tiny_obj_loader.h +++ b/tiny_obj_loader.h @@ -1789,6 +1789,10 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, unsigned int current_smoothing_id = 0; // Initial value. 0 means no smoothing. + int greatest_v_idx = -1; + int greatest_vn_idx = -1; + int greatest_vt_idx = -1; + shape_t shape; size_t line_num = 0; @@ -1907,6 +1911,10 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, return false; } + greatest_v_idx = greatest_v_idx > vi.v_idx ? greatest_v_idx : vi.v_idx; + greatest_vn_idx = greatest_vn_idx > vi.vn_idx ? greatest_vn_idx : vi.vn_idx; + greatest_vt_idx = greatest_vt_idx > vi.vt_idx ? greatest_vt_idx : vi.vt_idx; + face.vertex_indices.push_back(vi); size_t n = strspn(token, " \t\r"); token += n; @@ -2153,6 +2161,31 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, // Ignore unknown command. } + if (greatest_v_idx >= static_cast(v.size() / 3)) + { + if (err) { + std::stringstream ss; + ss << "WARN: Vertex indices out of bounds.\n" << std::endl; + (*err) += ss.str(); + } + } + if (greatest_vn_idx >= static_cast(vn.size() / 3)) + { + if (err) { + std::stringstream ss; + ss << "WARN: Vertex normal indices out of bounds.\n" << std::endl; + (*err) += ss.str(); + } + } + if (greatest_vt_idx >= static_cast(vt.size() / 2)) + { + if (err) { + std::stringstream ss; + ss << "WARN: Vertex texcoord indices out of bounds.\n" << std::endl; + (*err) += ss.str(); + } + } + bool ret = exportGroupsToShape(&shape, faceGroup, lineGroup, tags, material, name, triangulate, v); // exportGroupsToShape return false when `usemtl` is called in the last