Thread: How to detect if a pointer points to invalid memory? (Win32)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Uhh, IsBadReadPtr does not crash your program if the memory can't be read. It will return true if it's bad or false if not.
    There's no "easy" way to know if memory can be read or not. The best way is simply to return a NULL pointer if something failed.
    In C, I don't know of any way other than IsBadReadPtr to figure out if it's bad or not.
    In C++, you could always use try/catch, but it's not a gaurantee.

  2. #2
    Registered User
    Join Date
    May 2007
    Posts
    41
    Quote Originally Posted by Elysia View Post
    Uhh, IsBadReadPtr does not crash your program if the memory can't be read. It will return true if it's bad or false if not.
    I was thinking that too...then I've read this: http://geekswithblogs.net/ssimakov/a...5/19/5066.aspx

    What do you think about that article?

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The article is correct - if you arbitrarily check ALL kinds of pointers with IsBadXXXPtr then you could cause more problems than you will fix. You should not check stack-addresses with IsBadXXXPtr [1].

    Which goes back to Salem's question: What are you actually trying to "fix"? If your app is using some DLL that has a function that returns a pointer to some memory, it is a broken implementation if you can't read that memory. The next question is of course if you and the DLL agree on the size and usage of that memory - but the DLL is fundamentally broken if it doesn't return a valid memory address when that is what's expected of it.

    [1] Since the stack is ALWAYS readable and writable by your application, that should cause no problem. The next problem is of course to determine if an address is within the stack. The following code shows how you can do that:

    Code:
    char *baseStackPtr;
    void initStackCheck(void *p)
    {
       baseStackPtr = p;
    }
    
    int isStackPtr(void *p)
    {
        char dummy;
        char *topOfStack = &dummy;
        char *cp = p;
        
        if (cp > baseStackPtr && cp < topOfStack) 
          return 1;
        return 0;
    }
    
    int main()
    {
        int x;  // Any local variabe in main will do. 
        initStackCheck(&x);
        ....
         // your code goes here.
        
        return 0;
    }
    Also, you will only hit a stack guardpage if you are using a negative offset off the current stack position, since the stack grows towards zero - so the code is most likely broken in some bad way in the first place, if you hit that problem - because it means you are using some piece of memory that you don't currently own!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Suggestions on this C style code
    By Joelito in forum C Programming
    Replies: 11
    Last Post: 06-07-2007, 03:22 AM
  2. Replies: 8
    Last Post: 01-04-2006, 07:42 PM
  3. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  4. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM