缓冲区溢出异常(按fscanf_s)
Buffer Overrun Exception by fscanf_s
按照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));
安全版本就这么多... :)
相关文章:
- 'short int'持有的值溢出,但"自动"不会溢出?
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 大于65535的C++数组[size]引发不一致的溢出
- 为什么我在leetcode上收到AddressSanitizer:地址0x602000000058上的堆缓冲区溢出错误
- C++中无符号字符溢出
- 在 leetcode 上提交解决方案时出现堆栈缓冲区溢出错误
- 我的 int main() 中出现堆栈溢出错误
- 整数溢出,最大值为 pow(10,19)
- 获取隐式转换溢出从无符号到已签名的警告
- 使用 strcat 获取缓冲区溢出错误
- LeetCode 1:两和 - 地址清理器:堆缓冲区溢出地址
- 给定一个类型,如何派生一个泛型更广泛的类型(例如,用于溢出安全求和)?
- C++ 对象数组堆栈溢出
- 使用提升::lexical_cast捕获溢出
- C++ Unordered_set功能中的溢出
- 自定义排序函数中的堆溢出
- 使用向量的缓冲区溢出
- 有没有一种方法可以捕获进程中的堆栈溢出?C++Linux
- 如何修复此特征矩阵反演溢出错误?
- 自定义 std::fstream,std::filebuf 的溢出和下溢函数未为每个字符调用