用vc6编写的函数。
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; }现在我在vs08上升级它。项目构建正确,但是当我打开exe时会抛出异常
* HEAP CORRUPTION DETECTED * CRT检测到应用程序在堆缓冲区结束后写入内存。
当我调试我的应用程序时,控件在返回true后转到dbgheap.c的第2103行。
The function which were written in vc6.
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; }Now I upgrade it on vs08.The project build correctly but when I open exe it throw an exception
*HEAP CORRUPTION DETECTED * CRT Detected that the application wrote to memory after end of heap buffer.
When I debug the my application the the control goes to dbgheap.c at line no 2103 after return true.
最满意答案
问题出在这里:
dwRetCode = GetPrivateProfileString(cszVariables, strEntryName, "", strEntryValue.GetBufferSetLength(strEntryValue.GetLength()), iMaxEntryLen, strFilename);您传递大小为0的缓冲区( strEntryValue初始化为"" ),但是说它的大小是iMaxEntryLen 。 所以GetPrivateProfileString认为它有一个比它实际得到的更大的缓冲区,并且超出了它的范围。
升级后出现此错误的原因是,猜测是边界验证的改进。 这个bug也存在于VC6中,它只是没有检测到。
The problem is here:
dwRetCode = GetPrivateProfileString(cszVariables, strEntryName, "", strEntryValue.GetBufferSetLength(strEntryValue.GetLength()), iMaxEntryLen, strFilename);You pass a buffer of size 0 (strEntryValue is initialized to ""), but say its size is iMaxEntryLen. So GetPrivateProfileString thinks it has a much larger buffer than it actually got, and write beyond its bounds.
The reason you get this error after upgrading is, is guess, the improvement of the bounds validation. The bug was there in VC6 as well, it just wasn't detected.
更多推荐
发布评论