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

StackOverflow https://stackoverflow.com/questions/18421113

  •  26-06-2022
  •  | 
  •  

Question

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?

Était-ce utile?

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

Autres conseils

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));
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top