2012-07-12 2 views
0

vc6에서 작성된 함수입니다.프로젝트를 vc6에서 vc9로 업그레이드 한 후 힙 손상이 발견되었습니다.

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.The 프로젝트 빌드를 업그레이드하지만 exe 인 열 때 그것은

* 힙 손상이 CRT 응용 프로그램이 힙의 종료 후 메모리에 쓴 감지 * DETECTED 예외를 발생 완충기.

내 응용 프로그램을 디버깅 할 때 컨트롤은 dbgheap.c 라인을 반환합니다. 2103은 true를 반환 한 후입니다.

+0

나는 버퍼 rgbEntryNames가 너무 작다고 생각한다. – Jeeva

+0

나는 시도한다. ....... – vikky

+0

나는 시험했다. 그러나 문제는 같다. ...... – vikky

답변

4

문제는 여기에 있습니다 :

dwRetCode = GetPrivateProfileString(cszVariables, 
    strEntryName, 
    "", 
    strEntryValue.GetBufferSetLength(strEntryValue.GetLength()), 
    iMaxEntryLen, 
    strFilename); 

당신은 크기 0 (strEntryValue""으로 초기화된다)의 버퍼를 통과하지만, 그 크기는 iMaxEntryLen 말한다. 그래서 GetPrivateProfileString은 실제로 얻은 것보다 훨씬 더 큰 버퍼를 가지고 있으며 그 경계를 넘어서 쓰는 것으로 생각합니다.

업그레이드 후이 오류가 발생하는 이유는 경계 유효성 검사의 개선이라고 생각됩니다. 이 버그는 VC6에도 있었지만 발견되지 않았습니다.

관련 문제