简体   繁体   中英

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?

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));

The extra X is your problem:

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

Windows A, W and X suck.

Avoiding Macros :

template <typename T>
inline void zero_memory(T& m) {
    std::memset(&T, 0, sizeof(T));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM