Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4b62a19

Browse files
committed
Skip whitespaces between usemtl and name. Fixes tinyobjloader#246
1 parent bfdb443 commit 4b62a19

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

tests/tester.cc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,6 +1252,34 @@ void test_mtl_searchpaths_issue244() {
12521252
TEST_CHECK(4 == shapes[1].mesh.indices[0].vertex_index);
12531253
}
12541254

1255+
void test_usemtl_whitespace_issue246() {
1256+
tinyobj::attrib_t attrib;
1257+
std::vector<tinyobj::shape_t> shapes;
1258+
std::vector<tinyobj::material_t> materials;
1259+
1260+
std::string warn;
1261+
std::string err;
1262+
bool ret = tinyobj::LoadObj(
1263+
&attrib, &shapes, &materials, &warn, &err,
1264+
"../models/issue-246-usemtl-whitespace.obj",
1265+
gMtlBasePath);
1266+
1267+
TEST_CHECK(warn.empty());
1268+
1269+
if (!warn.empty()) {
1270+
std::cout << "WARN: " << warn << std::endl;
1271+
}
1272+
1273+
if (!err.empty()) {
1274+
std::cerr << "ERR: " << err << std::endl;
1275+
}
1276+
1277+
TEST_CHECK(true == ret);
1278+
TEST_CHECK(1 == shapes.size());
1279+
TEST_CHECK(1 == materials.size());
1280+
TEST_CHECK(0 == shapes[0].mesh.material_ids[0]);
1281+
}
1282+
12551283
// Fuzzer test.
12561284
// Just check if it does not crash.
12571285
// Disable by default since Windows filesystem can't create filename of afl
@@ -1347,4 +1375,6 @@ TEST_LIST = {
13471375
test_usemtl_then_o_issue235},
13481376
{"mtl_searchpaths_issue244",
13491377
test_mtl_searchpaths_issue244},
1378+
{"usemtl_whitespece_issue246",
1379+
test_usemtl_whitespace_issue246},
13501380
{NULL, NULL}};

tiny_obj_loader.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2353,19 +2353,17 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
23532353
}
23542354

23552355
// use mtl
2356-
if ((0 == strncmp(token, "usemtl", 6)) && IS_SPACE((token[6]))) {
2357-
token += 7;
2358-
std::stringstream ss;
2359-
ss << token;
2360-
std::string namebuf = ss.str();
2356+
if ((0 == strncmp(token, "usemtl", 6))) {
2357+
token += 6;
2358+
std::string namebuf = parseString(&token);
23612359

23622360
int newMaterialId = -1;
23632361
if (material_map.find(namebuf) != material_map.end()) {
23642362
newMaterialId = material_map[namebuf];
23652363
} else {
23662364
// { error!! material not found }
23672365
if (warn) {
2368-
(*warn) += "material [ " + namebuf + " ] not found in .mtl\n";
2366+
(*warn) += "material [ '" + namebuf + "' ] not found in .mtl\n";
23692367
}
23702368
}
23712369

0 commit comments

Comments
 (0)