@@ -1789,6 +1789,10 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
17891789 unsigned int current_smoothing_id =
17901790 0 ; // Initial value. 0 means no smoothing.
17911791
1792+ int greatest_v_idx = -1 ;
1793+ int greatest_vn_idx = -1 ;
1794+ int greatest_vt_idx = -1 ;
1795+
17921796 shape_t shape;
17931797
17941798 size_t line_num = 0 ;
@@ -1907,6 +1911,10 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
19071911 return false ;
19081912 }
19091913
1914+ greatest_v_idx = greatest_v_idx > vi.v_idx ? greatest_v_idx : vi.v_idx ;
1915+ greatest_vn_idx = greatest_vn_idx > vi.vn_idx ? greatest_vn_idx : vi.vn_idx ;
1916+ greatest_vt_idx = greatest_vt_idx > vi.vt_idx ? greatest_vt_idx : vi.vt_idx ;
1917+
19101918 face.vertex_indices .push_back (vi);
19111919 size_t n = strspn (token, " \t\r " );
19121920 token += n;
@@ -2153,6 +2161,31 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
21532161 // Ignore unknown command.
21542162 }
21552163
2164+ if (greatest_v_idx >= static_cast <int >(v.size () / 3 ))
2165+ {
2166+ if (err) {
2167+ std::stringstream ss;
2168+ ss << " WARN: Vertex indices out of bounds.\n " << std::endl;
2169+ (*err) += ss.str ();
2170+ }
2171+ }
2172+ if (greatest_vn_idx >= static_cast <int >(vn.size () / 3 ))
2173+ {
2174+ if (err) {
2175+ std::stringstream ss;
2176+ ss << " WARN: Vertex normal indices out of bounds.\n " << std::endl;
2177+ (*err) += ss.str ();
2178+ }
2179+ }
2180+ if (greatest_vt_idx >= static_cast <int >(vt.size () / 2 ))
2181+ {
2182+ if (err) {
2183+ std::stringstream ss;
2184+ ss << " WARN: Vertex texcoord indices out of bounds.\n " << std::endl;
2185+ (*err) += ss.str ();
2186+ }
2187+ }
2188+
21562189 bool ret = exportGroupsToShape (&shape, faceGroup, lineGroup, tags, material,
21572190 name, triangulate, v);
21582191 // exportGroupsToShape return false when `usemtl` is called in the last
0 commit comments