Visual c++ 将项目从vc6升级到vc9后检测到堆损坏
用vc6编写的函数Visual c++ 将项目从vc6升级到vc9后检测到堆损坏,visual-c++,mfc,visual-c++-6,Visual C++,Mfc,Visual C++ 6,用vc6编写的函数 bool CProductionTestDlg::GetVariables(CString strFilename, CMapStringToOb *cVariableMap) { int iMaxEntryLen = 1000; //char rgbEntryNames[1000]; //previous char *rgbEntryNames = (char*)malloc(iMaxEntryLen
bool CProductionTestDlg::GetVariables(CString strFilename, CMapStringToOb *cVariableMap)
{
int iMaxEntryLen = 1000;
//char rgbEntryNames[1000]; //previous
char *rgbEntryNames = (char*)malloc(iMaxEntryLen * sizeof(int)); //Now
CString strEntryName = "";
CString strEntryValue = "";
UINT uiSeperator = 0;
ULONG dwRetCode, dwSizeOfReturn;
dwSizeOfReturn = GetPrivateProfileString(cszVariables,
NULL,
"",
rgbEntryNames,
iMaxEntryLen,
strFilename);
while ( uiSeperator < dwSizeOfReturn )
{
strEntryName.Format("%s", &rgbEntryNames[uiSeperator]);
uiSeperator += strEntryName.GetLength() + 1;
CString *strValue = new CString();
dwRetCode = GetPrivateProfileString(cszVariables,
strEntryName,
"",
strEntryValue.GetBufferSetLength(strEntryValue.GetLength()),
iMaxEntryLen,
strFilename);
strValue->Format("%s", strEntryValue);
cVariableMap->SetAt(strEntryName, (CObject*)strValue);
}
return true;
}
bool CProductionTestDlg::GetVariables(CString strFilename,CMapStringToOb*cVariableMap)
{
int iMaxEntryLen=1000;
//char rgbEntryNames[1000];//上一个
char*rgbEntryNames=(char*)malloc(iMaxEntryLen*sizeof(int));//现在
CString strengthryname=“”;
CString strengthyvalue=“”;
UINT uiSeperator=0;
ULONG dwRetCode,dwSizeOfReturn;
dwSizeOfReturn=GetPrivateProfileString(CSZ变量,
无效的
"",
rgbEntryNames,
伊玛克森特伦,
strFilename);
while(uiSeperator格式(“%s”,强度值);
cVariableMap->SetAt(strengtryname,(CObject*)标准值);
}
返回true;
}
现在我在vs08上升级了它。项目生成正确,但当我打开exe时,它会抛出一个异常
*检测到堆损坏*CRT检测到应用程序在堆缓冲区结束后写入内存
当我调试我的应用程序时,控件在返回true后转到第2103行的dbgheap.c。问题在于:
dwRetCode = GetPrivateProfileString(cszVariables,
strEntryName,
"",
strEntryValue.GetBufferSetLength(strEntryValue.GetLength()),
iMaxEntryLen,
strFilename);
您传递一个大小为0的缓冲区(strEntryValue
被初始化为”
),但说它的大小是iMaxEntryLen
。因此,GetPrivateProfileString
认为它的缓冲区比实际得到的要大得多,并且写得超出了它的界限
升级后出现此错误的原因是边界验证的改进。该漏洞也存在于VC6中,只是没有被检测到。问题在于:
dwRetCode = GetPrivateProfileString(cszVariables,
strEntryName,
"",
strEntryValue.GetBufferSetLength(strEntryValue.GetLength()),
iMaxEntryLen,
strFilename);
您传递一个大小为0的缓冲区(strEntryValue
被初始化为”
),但说它的大小是iMaxEntryLen
。因此,GetPrivateProfileString
认为它的缓冲区比实际得到的要大得多,并且写得超出了它的界限
升级后出现此错误的原因是边界验证的改进。该漏洞也存在于VC6中,只是没有被检测到。我认为缓冲区rgbEntryNames太小了,我尝试过了,但问题仍然是一样的……@Jeeva看看我编辑过的问题……@vikky使用调用堆栈窗口转到CPProductionTestDLG::GetVariables的上下文。通过这种方式,您将看到函数中的哪一行导致了异常。我认为缓冲区rgbEntryNames太小了,我尝试过了,但问题仍然是一样的……@Jeeva查看我已编辑的问题……@vikky使用调用堆栈窗口转到CProductionTestDlg::GetVariables的上下文。这样,您将看到函数中的哪一行导致了异常。