fscanf_s的缓冲区溢出异常

时间:2013-10-01 23:33:13

标签: c++

在openGL初学者教程之后,我已经编写了自己的.OBJ文件加载器。在编写之后,通常会进行编译过程,这在C ++中很烦人且令人作呕。我在我的一个方法中遇到了一个未处理的异常,这里是代码:

float* Loader::LoadObj(const char* filePath){
//Declaration of v, vt and vn temporary container buffers for parsing
vector<glm::vec3> vertcies;
vector<glm::vec2> uvs;
vector<glm::vec3> normals;
vector<fvert> facevertcies;

//open the file
FILE* objFile;
fopen_s(&objFile, filePath,"r");
if( objFile == NULL )
{
    printf("Impossible to open the file ! Are you in the right path ?\n");
}

            char line[128]; // where to store the first string read from each line

            //other containers
            glm::vec3 vertex;
            glm::vec2 uv;
            glm::vec3 normal;
            fvert fv1,fv2,fv3;

            // for each line in the obj file
            while(true)
            {

                // read the first word of the line
                int lineId = fscanf_s(objFile,"%s",line);                   
                        if (lineId == EOF)  
                            break;
                // parse line by line
                if ( strcmp( line, "v" ) == 0 )
                {
                        fscanf_s(objFile, "%f %f %f\n", &vertex.x, &vertex.y, &vertex.z);
                        vertcies.push_back(vertex);                             
                }
                else if ( strcmp( line, "vt" ) == 0 )
                {       
                        fscanf_s(objFile, "%f %f\n", &uv.x, &uv.y);
                        uvs.push_back(uv);                          
                }
                else if ( strcmp( line, "vn" ) == 0 )
                {
                        fscanf_s(objFile,"%f %f %f\n", &normal.x, &normal.y, &normal.z );
                        normals.push_back(normal);
                }... //method continues}}

代码不完整,但其他部分无关紧要。 LoadObj(char *)方法尝试打开obj文件,然后进入while循环,遍历文件的所有行以进行解析。通过断点,我设法推断出在循环中第一次调用fscanf_s时会发生“堆栈缓冲区溢出”异常(文件打开正常)。我尝试用空尝试抓住它,但没有成功。

请有人帮我理解导致此异常的原因,并通过解决方案补充他们的答案。这是我在这个论坛上的第一篇文章,希望我没有违反任何规则。请帮助新手。这是CallStack。

    XXX.exe!__report_gsfailure(unsigned __int64 StackCookie) Line 147   C
XXX.exe!__GSHandlerCheckCommon(void * EstablisherFrame, _DISPATCHER_CONTEXT * DispatcherContext, _GS_HANDLER_DATA * GSHandlerData) Line 189 C
XXX.exe!__GSHandlerCheck_EH(_EXCEPTION_RECORD * ExceptionRecord, void * EstablisherFrame, _CONTEXT * ContextRecord, _DISPATCHER_CONTEXT * DispatcherContext) Line 96    C
ntdll.dll!000007fc400f9f4d()    Unknown
ntdll.dll!000007fc400fb220()    Unknown
ntdll.dll!000007fc400e4bba()    Unknown
msvcr110d.dll!memset() Line 161 Unknown
msvcr110d.dll!_input_s_l(_iobuf * stream, const unsigned char * format, localeinfo_struct * plocinfo, char * arglist) Line 939  C++
msvcr110d.dll!vfscanf(int (_iobuf *, const unsigned char *, localeinfo_struct *, char *) * inputfn, _iobuf * stream, const char * format, localeinfo_struct * plocinfo, char * arglist) Line 62 C
msvcr110d.dll!fscanf_s(_iobuf * stream, const char * format, ...) Line 132  C

Bonus问题:我还在这里阅读http://blog.markloiseau.com/2012/02/two-safer-alternatives-to-scanf/我的代码可以通过使用char *而不是fixed char数组并将%ms传递给fscanf来改进,fscanf动态分配内存用于存储。有人可以告诉我,如何在我的代码中实现它?这种可能性很有吸引力,尽管我甚至无法得到该死的原件。谢谢那些能够提供帮助的人。

1 个答案:

答案 0 :(得分:1)

int lineId = fscanf_s(objFile,"%s",line); 

应该可能改为:

int lineId = fscanf_s(objFile,"%s",line,sizeof(line)); 

安全版本......:)