Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Replacements for IsBadReadPtr and IsBadWritePtr

Replacements for IsBadReadPtr and IsBadWritePtr

Scheduled Pinned Locked Moved C / C++ / MFC
question
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Blake Miller
    wrote on last edited by
    #1

    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.

    M L 2 Replies Last reply
    0
    • B Blake Miller

      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.

      M Offline
      M Offline
      Mike Dimmick
      wrote on last edited by
      #2

      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

      B 1 Reply Last reply
      0
      • M Mike Dimmick

        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

        B Offline
        B Offline
        Blake Miller
        wrote on last edited by
        #3

        Yes, I know that. I was asking if anyone had already written replacements or knew of anything else.

        M 1 Reply Last reply
        0
        • B Blake Miller

          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.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • B Blake Miller

            Yes, I know that. I was asking if anyone had already written replacements or knew of anything else.

            M Offline
            M Offline
            Mike Dimmick
            wrote on last edited by
            #5

            #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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups