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] = '
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);
}
Comments
- Anonymous
April 16, 2004
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? - Anonymous
April 16, 2004
> 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. - Anonymous
April 16, 2004
Gotcha - thanks for the clarification!