I saw the code below in @constm answer to this question,
Most efficient replacement for IsBadReadPtr?
bool IsBadReadPtr(void* p)
{
MEMORY_BASIC_INFORMATION mbi = {0};
if (::VirtualQuery(p, &mbi, sizeof(mbi)))
{
DWORD mask = (PAGE_READONLY|PAGE_READWRITE|PAGE_WRITECOPY|PAGE_EXECUTE_READ|PAGE_EXECUTE_READWRITE|PAGE_EXECUTE_WRITECOPY);
bool b = !(mbi.Protect & mask);
// check the page is not a guard page
if (mbi.Protect & (PAGE_GUARD|PAGE_NOACCESS)) b = true;
return b;
}
return true;
}
I am using Windows and coding in Visual Studio 2013. The code looks fine to me and I can see that it would overcome the problem in isBadReadPtr
of accessing page guards, but I would like to understand how it would overcome the problem of multi-threading?
Is this code a good substitute for isBadReadPtr
? Also looks like this code should return false
for me to understand that the ptr
I pass is valid.
Also if I pass a object of a class, how does it know the size of the object and check the whole space? Maybe I didnt understand VirtualQuery well.
Please let me know if what I understood is right.
TIA!