将项目从vc6升级到vc9后检测到HEAP CORRUPTION(HEAP CORRUPTION DETECTED after upgrade the project from vc6 to vc9)

编程入门 行业动态 更新时间:2024-10-12 05:54:41
将项目从vc6升级到vc9后检测到HEAP CORRUPTION(HEAP CORRUPTION DETECTED after upgrade the project from vc6 to vc9)

用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.

更多推荐

本文发布于:2023-07-30 14:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1338848.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:升级到   检测到   项目   upgrade   project

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!