0

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!

Community
  • 1
  • 1
quickdraw
  • 227
  • 2
  • 11
  • 2
    fyi: Old New Thing: "IsBadXxxPtr should really be called CrashProgramRandomly" https://blogs.msdn.microsoft.com/oldnewthing/20060927-07/?p=29563 – Richard Critten Mar 31 '17 at 06:16
  • @RichardCritten, yes I am aware of that. Thats the reason I am looking for an fixed alternative. This is alternative fine?\ – quickdraw Mar 31 '17 at 06:20
  • There's not going to be a "fix". It's not broken. Did you read and understand the *logic* behind Raymond's post? – Cody Gray Dec 22 '20 at 13:45

0 Answers0