Search code examples
c++mfcstackwindowsversion

Run-Time Check Failure #2 - stack around the variable 'osvi' was corrupted on mfc application


I've been searching around the internet and I have no idea why this happens, it's not really an obvious array issue.

Here's the function:

BOOL IsOsCompatible()
{
    BOOL retVal = 0;
    OSVERSIONINFO osvi;
    ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
    osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
    GetVersionEx(&osvi);
    if(osvi.dwMajorVersion == 6)
    {
        if(osvi.dwMinorVersion == 0)
        {
            if(SendErrorM("This program has not been tested on Windows Vista(TM).\nAre you sure you want to use it?",MB_YESNO) == IDYES)
                retVal = 1;
        }
        else if(osvi.dwMinorVersion == 1)
        { 
            retVal = 1;
        }
        else if(osvi.dwMinorVersion == 2)
        {
            if(SendErrorM("This program has not been tested on Windows 8(TM).\nAre you sure you want to use it?",MB_YESNO) == IDYES)
                retVal = 1;
        }
    }
    else
        SendErrorM("Your windows verison is incompatible with the minimum requirements of this application.",NULL);

    return retVal;

}

Any ideas?


Solution

  • An OSVERSIONINFOEX is larger than an OSVERSIONINFO, so

        ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
    

    will write zeroes "outside" (around) osvi.

    You want

    OSVERSIONINFOEX osvi;
    ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
    

    or (often safer)

    OSVERSIONINFOEX osvi;
    ZeroMemory(&osvi, sizeof(osvi));