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

Skip to content

Commit 1adfc79

Browse files
committed
Fix sscanf_s seg fault on windows. Fixes tinyobjloader#41
1 parent a2851fd commit 1adfc79

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

tiny_obj_loader.cc

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@
55
//
66

77
//
8-
// version 0.9.9: Replace atof() with custom parser.
9-
// version 0.9.8: Fix multi-materials(per-face material ID).
10-
// version 0.9.7: Support multi-materials(per-face material ID) per
8+
// version 0.9.10: Fix seg fault on windows.
9+
// version 0.9.9 : Replace atof() with custom parser.
10+
// version 0.9.8 : Fix multi-materials(per-face material ID).
11+
// version 0.9.7 : Support multi-materials(per-face material ID) per
1112
// object/group.
12-
// version 0.9.6: Support Ni(index of refraction) mtl parameter.
13-
// Parse transmittance material parameter correctly.
14-
// version 0.9.5: Parse multiple group name.
15-
// Add support of specifying the base path to load material file.
16-
// version 0.9.4: Initial suupport of group tag(g)
17-
// version 0.9.3: Fix parsing triple 'x/y/z'
18-
// version 0.9.2: Add more .mtl load support
19-
// version 0.9.1: Add initial .mtl load support
20-
// version 0.9.0: Initial
13+
// version 0.9.6 : Support Ni(index of refraction) mtl parameter.
14+
// Parse transmittance material parameter correctly.
15+
// version 0.9.5 : Parse multiple group name.
16+
// Add support of specifying the base path to load material file.
17+
// version 0.9.4 : Initial suupport of group tag(g)
18+
// version 0.9.3 : Fix parsing triple 'x/y/z'
19+
// version 0.9.2 : Add more .mtl load support
20+
// version 0.9.1 : Add initial .mtl load support
21+
// version 0.9.0 : Initial
2122
//
2223

2324
#include <cstdlib>
@@ -36,6 +37,8 @@
3637

3738
namespace tinyobj {
3839

40+
#define TINYOBJ_SSCANF_BUFFER_SIZE (4096)
41+
3942
struct vertex_index {
4043
int v_idx, vt_idx, vn_idx;
4144
vertex_index(){};
@@ -457,10 +460,10 @@ std::string LoadMtl(std::map<std::string, int> &material_map,
457460
InitMaterial(material);
458461

459462
// set new mtl name
460-
char namebuf[4096];
463+
char namebuf[TINYOBJ_SSCANF_BUFFER_SIZE];
461464
token += 7;
462465
#ifdef _MSC_VER
463-
sscanf_s(token, "%s", namebuf);
466+
sscanf_s(token, "%s", namebuf, _countof(namebuf));
464467
#else
465468
sscanf(token, "%s", namebuf);
466469
#endif
@@ -748,10 +751,10 @@ std::string LoadObj(std::vector<shape_t> &shapes,
748751
// use mtl
749752
if ((0 == strncmp(token, "usemtl", 6)) && isSpace((token[6]))) {
750753

751-
char namebuf[4096];
754+
char namebuf[TINYOBJ_SSCANF_BUFFER_SIZE];
752755
token += 7;
753756
#ifdef _MSC_VER
754-
sscanf_s(token, "%s", namebuf);
757+
sscanf_s(token, "%s", namebuf, _countof(namebuf));
755758
#else
756759
sscanf(token, "%s", namebuf);
757760
#endif
@@ -775,10 +778,10 @@ std::string LoadObj(std::vector<shape_t> &shapes,
775778

776779
// load mtl
777780
if ((0 == strncmp(token, "mtllib", 6)) && isSpace((token[6]))) {
778-
char namebuf[4096];
781+
char namebuf[TINYOBJ_SSCANF_BUFFER_SIZE];
779782
token += 7;
780783
#ifdef _MSC_VER
781-
sscanf_s(token, "%s", namebuf);
784+
sscanf_s(token, "%s", namebuf, _countof(namebuf));
782785
#else
783786
sscanf(token, "%s", namebuf);
784787
#endif
@@ -841,10 +844,10 @@ std::string LoadObj(std::vector<shape_t> &shapes,
841844
shape = shape_t();
842845

843846
// @todo { multiple object name? }
844-
char namebuf[4096];
847+
char namebuf[TINYOBJ_SSCANF_BUFFER_SIZE];
845848
token += 2;
846849
#ifdef _MSC_VER
847-
sscanf_s(token, "%s", namebuf);
850+
sscanf_s(token, "%s", namebuf, _countof(namebuf));
848851
#else
849852
sscanf(token, "%s", namebuf);
850853
#endif

0 commit comments

Comments
 (0)