Skip to content

Commit b85714b

Browse files
committed
Skip parsing incomplete or invalid face definition(e.g. f definition only contains 1 or 2 indices).
1 parent e060b4f commit b85714b

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

models/invalid-face-definition.mtl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
newmtl white
2+
Ka 0 0 0
3+
Kd 1 1 1
4+
Ks 0 0 0
5+
6+
newmtl red
7+
Ka 0 0 0
8+
Kd 1 0 0
9+
Ks 0 0 0
10+
11+
newmtl green
12+
Ka 0 0 0
13+
Kd 0 1 0
14+
Ks 0 0 0
15+
16+
newmtl blue
17+
Ka 0 0 0
18+
Kd 0 0 1
19+
Ks 0 0 0
20+
21+
newmtl light
22+
Ka 20 20 20
23+
Kd 1 1 1
24+
Ks 0 0 0

models/invalid-face-definition.obj

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
mtllib invalid-face-definition.mtl
2+
3+
v 0.000000 2.000000 2.000000
4+
v 0.000000 0.000000 2.000000
5+
v 2.000000 0.000000 2.000000
6+
v 2.000000 2.000000 2.000000
7+
v 0.000000 2.000000 0.000000
8+
v 0.000000 0.000000 0.000000
9+
v 2.000000 0.000000 0.000000
10+
v 2.000000 2.000000 0.000000
11+
# 8 vertices
12+
13+
g front cube
14+
usemtl white
15+
f 1
16+
g back cube
17+
# expects white material
18+
f 8 7

tests/tester.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,24 @@ TEST_CASE("smoothing-group", "[Issue162]") {
751751

752752
}
753753

754+
TEST_CASE("invalid-face-definition", "[face]") {
755+
tinyobj::attrib_t attrib;
756+
std::vector<tinyobj::shape_t> shapes;
757+
std::vector<tinyobj::material_t> materials;
758+
759+
std::string err;
760+
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/invalid-face-definition.obj", gMtlBasePath);
761+
762+
763+
if (!err.empty()) {
764+
std::cerr << "[face] " << err << std::endl;
765+
}
766+
767+
REQUIRE(true == ret);
768+
REQUIRE(1 == shapes.size());
769+
REQUIRE(0 == shapes[0].mesh.indices.size());
770+
}
771+
754772
#if 0
755773
int
756774
main(

tiny_obj_loader.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,11 @@ static bool exportFaceGroupToShape(shape_t *shape,
10231023
for (size_t i = 0; i < faceGroup.size(); i++) {
10241024
const face_t &face = faceGroup[i];
10251025

1026+
if (face.vertex_indices.size() < 3) {
1027+
// Face must have 3+ vertices.
1028+
continue;
1029+
}
1030+
10261031
vertex_index_t i0 = face.vertex_indices[0];
10271032
vertex_index_t i1(-1);
10281033
vertex_index_t i2 = face.vertex_indices[1];

0 commit comments

Comments
 (0)