Replacements for IsBadReadPtr and IsBadWritePtr
-
Has anybody already written some? I am thinking variations on data returned from VirtualQuery could do the trick. Oh yeah, because IsBadReadPtr and IsBadWritePtr won't do anything on Windows Vista, according to MS documentation.
-
Has anybody already written some? I am thinking variations on data returned from VirtualQuery could do the trick. Oh yeah, because IsBadReadPtr and IsBadWritePtr won't do anything on Windows Vista, according to MS documentation.
Raymond Chen: IsBadXxxPtr should really be called CrashProgramRandomly[^]. They've been deactivated as they're a common source of problems.
DoEvents: Generating unexpected recursion since 1991
-
Raymond Chen: IsBadXxxPtr should really be called CrashProgramRandomly[^]. They've been deactivated as they're a common source of problems.
DoEvents: Generating unexpected recursion since 1991
Yes, I know that. I was asking if anyone had already written replacements or knew of anything else.
-
Has anybody already written some? I am thinking variations on data returned from VirtualQuery could do the trick. Oh yeah, because IsBadReadPtr and IsBadWritePtr won't do anything on Windows Vista, according to MS documentation.
For a read pointer all we need to know is if VirtualQuery can read it. So you could do something like this:
BOOL IsBadReadPointerCheck(char *p, size_t size) { MEMORY_BASIC_INFORMATION mbi; for (void *pStop = p + size; p < pStop;p =p+mbi.RegionSize) { if(!VirtualQuery(p,&mbi,sizeof(mbi))) { return FALSE; } } return TRUE; }
For a write pointer we would need to iterate through the entire range and check the bits stored in the Protect member of the MEMORY_BASIC_INFORMATION structure. Something like this:
BOOL IsBadWritePointer(char *p, size_t size) { BOOL bRet = FALSE; void *pStop = p+size; MEMORY_BASIC_INFORMATION mbi; for(p;p < pStop;++p)) { if(VirtualQueryEx(GetCurrentProcess(),(LPVOID)p,&mbi,sizeof(MEMORY_BASIC_INFORMATION))) { if(!(((mbi.Protect & PAGE_READWRITE) == PAGE_READWRITE) || ((mbi.Protect & PAGE_EXECUTE_READWRITE) == PAGE_EXECUTE_READWRITE))) { return TRUE; } } } return bRet; }
Lets say that you have a pointer to a member function and you want to validate the member function pointer. You might do something like this:
MEMORY_BASIC_INFORMATION mbi; if(VirtualQueryEx(GetCurrentProcess(),(LPVOID)pInstructions,&mbi,sizeof(MEMORY_BASIC_INFORMATION))) { if(mbi.Protect & PAGE_EXECUTE_READ && mbi.State & MEM_COMMIT && mbi.AllocationProtect & PAGE_EXECUTE_WRITECOPY && mbi.Type & MEM_IMAGE) { (*this.*MemberFunction)(val); return TRUE; } }
*Disclaimer Edit* I have not had time to test these functions as I am currently too busy. These functions were removed from some experimental code I was working on some time back. Use/Modify them at your own risk. Just by looking over them I can see they need to have additional error handling. Best Wishes, -David Delaune
modified on Friday, May 2, 2008 3:05 PM
-
Yes, I know that. I was asking if anyone had already written replacements or knew of anything else.
#define IsBadReadPtr(x) ((x) == NULL)
#define IsBadWritePtr(x) ((x) == NULL)These functions are that bad. An immediate null pointer check is about as good as it's going to get.
DoEvents: Generating unexpected recursion since 1991