在以下代码中返回时,我收到此运行时检查失败。我相信类似的代码在程序的其他地方运行良好。有任何想法吗?
String GetVariableName(CString symbol, CString filepath)
{
char acLine[512];
char acPreviousLine[512];
CString csFile;
FILE *fp;
csFile.Format("%svariables.txt", filepath);
fp = fopen(csFile, "r");
if (! fp)
return("");
for (;;)
{
strcpy(acPreviousLine, acLine);
// NULL means we are out of lines in the file.
if (myfgets(acLine, 511, fp) == NULL)
break;
// "END" indicates end of file
if (! strcmp(acLine, "END"))
break;
if (! strcmp(acLine, csVarSymbol))
{
// Previous line should be variable name
fclose(fp);
// Following line results in Check Failure while in Debug mode
return(acPreviousLine);
}
}
fclose(fp);
return("");
}
上面的示例中没有变量“x”,但我假设您编辑了错误消息!
acLine 未初始化,因此第一次将其复制到 acPreviousLine 时,您正在复制堆栈上的所有内容。在某些情况下,这可能会导致缓冲区溢出,从而导致堆栈损坏 - 并非全部,因为您可能很幸运,在达到 512 字节之前在 acLine 中找到了 null。
返回时会检查堆栈是否损坏,因为在所有堆栈变量周围插入了保护字(在此平台和构建配置上 - 我假设是在 Windows 上,在调试模式下在 VS 上编译)来检查该问题。
将 acLine[0] 初始化为 0。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)