缓冲区溢出异常(按fscanf_s)

Buffer Overrun Exception by fscanf_s

本文关键字:fscanf 溢出 异常 缓冲区      更新时间:2023-10-16

按照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 %fn", &vertex.x, &vertex.y, &vertex.z);
                        vertcies.push_back(vertex);                             
                }
                else if ( strcmp( line, "vt" ) == 0 )
                {       
                        fscanf_s(objFile, "%f %fn", &uv.x, &uv.y);
                        uvs.push_back(uv);                          
                }
                else if ( strcmp( line, "vn" ) == 0 )
                {
                        fscanf_s(objFile,"%f %f %fn", &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

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

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

应该可能更改为:

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

安全版本就这么多... :)