Fabulous Adventures In Coding

Eric Lippert's Blog

assert.cpp

#include "headers.h"

static BOOL IsGoodPtr(void * pv, ULONG cb, DWORD dwFlags)
{
    DWORD dwSize;
    MEMORY_BASIC_INFORMATION meminfo;

    if (NULL == pv)
        return FALSE;

    memset(&meminfo, 0x00, sizeof meminfo);
    dwSize = VirtualQuery(pv, &meminfo, sizeof meminfo);
    // If pv is a kernel-mode address then this may return zero for security reasons.
    // In that event it is certainly NOT a valid read pointer.

    if (0 == dwSize)
        return FALSE;

    if (MEM_COMMIT != meminfo.State)
        return FALSE;

    if (0 == (meminfo.Protect & dwFlags))
        return FALSE;

    if (cb > meminfo.RegionSize)
        return FALSE;

    if ((unsigned)((char *)pv - (char *)meminfo.BaseAddress) > (unsigned)(meminfo.RegionSize - cb))
        return FALSE;

    return TRUE;
}

BOOL IsGoodReadPtr(void * pv, ULONG cb)
{
    return IsGoodPtr(pv, cb, PAGE_READONLY | PAGE_READWRITE | PAGE_WRITECOPY |
        PAGE_EXECUTE_READ | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY);
}

BOOL IsGoodWritePtr(void * pv, ULONG cb)
{
    return IsGoodPtr(pv, cb, PAGE_READWRITE | PAGE_WRITECOPY |
        PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY);
}

BOOL Debugger()
{
#if DEBUG // {
    DebugBreak();
#endif // DEBUG }
    return TRUE;
}

BOOL AssertProc(const char * pszFile, LONG lwLine, const char * pszMsg)
{
    LONG sid;
    const int cch = 512;
    char pch[cch];
    _snprintf(pch, cch - 1, "Assert (%s line %ld): %s", pszFile, lwLine, pszMsg);
    pch[cch - 1] = '\0';
    sid = MessageBoxA(NULL, pch,
        "Assert! (Y = Ignore, N = Debugger, C = Quit)",
        MB_SYSTEMMODAL | MB_YESNOCANCEL | MB_ICONHAND);
    switch (sid)
    {
    default:
        // Ignore
        break;
    case IDNO:
        // Debug
        return TRUE;
    case IDCANCEL:
        // Quit
        FatalAppExitA(0, "Fatal Error Termination");
        break;
    }
    return FALSE;
}

BOOL IsValidReadString(const WCHAR * psz)
{
    if (NULL == psz)
        return FALSE;
    return IsGoodReadPtr((void*)psz, 2);
}

BOOL IsValidReadStringN(const WCHAR * psz)
{
    if (NULL == psz)
        return TRUE;
    return IsValidReadString(psz);
}


 

Published Wednesday, March 31, 2004 6:16 PM by Eric Lippert
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Fabulous Adventures In Coding said:

April 1, 2004 2:01 AM
 

Kim Gräsman said:

Hi Eric,

You've left me wondering... Is IsGoodReadPtr really doing the right thing? It passes in a whole host of flags indicating Write operations - won't this require more permissions than necessary from the memory page in question?
April 16, 2004 8:36 AM
 

Eric Lippert said:

> Is IsGoodReadPtr really doing the right thing?

Yes.

> It passes in a whole host of flags indicating Write operations - won't this require more permissions than necessary from the memory page in question?

No, you're misreading the code. The code means "a page is a good read page if it is read-only OR read-write..."

Those flags are not things that the caller REQUIRES as proof of being a good page, those are things that it ACCEPTS as proof. Any one will do.
April 16, 2004 9:57 AM
 

Kim Gräsman said:

Gotcha - thanks for the clarification!
April 16, 2004 11:55 AM

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required
Submit

About Eric Lippert

Eric Lippert is a senior developer on the Microsoft C# compiler team. Before that he worked on the framework of Visual Studio Tools For Office. Before that, he worked on the compilers, runtimes and tools for VBScript, JScript, Windows Script Host and other Microsoft Scripting technologies. He lives in Seattle and spends his free time editing books about programming languages, playing the piano, and trying to keep his tiny sailboat upright in Puget Sound.

This Blog

Syndication


© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker