Most efficient replacement for IsBadReadPtr?

前端 未结 10 1242
你的背包
你的背包 2020-12-06 06:45

I have some Visual C++ code that receives a pointer to a buffer with data that needs to be processed by my code and the length of that buffer. Due to a bug outside my contro

相关标签:
10条回答
  • 2020-12-06 07:27

    a thread-safe solution would be nice

    I'm guessing it's only IsBadWritePtr that isn't thread-safe.

    just doing a memcpy inside an exception handler

    This is effectively what IsBadReadPtr is doing ... and if you did it in your code, then your code would have the same bug as the IsBadReadPtr implementation: http://blogs.msdn.com/oldnewthing/archive/2006/09/27/773741.aspx

    --Edit:--

    The only problem with IsBadReadPtr that I've read about is that the bad pointer might be pointing to (and so you might accidentally touch) a stack's guard page. Perhaps you could avoid this problem (and therefore use IsBadReadPtr safely), by:

    • Know what threads are running in your process
    • Know where the threads' stacks are, and how big they are
    • Walk down each stack, delberately touching each page of the stack at least once, before you begin to call isBadReadPtr

    Also, the some of the comments associated with the URL above also suggest using VirtualQuery.

    0 讨论(0)
  • 2020-12-06 07:27

    Any implementation of checking the validity of memory is subject to the same constriants that make IsBadReadPtr fail. Can you post an example callstack of where you want to check the validity of memory of a pointer passed to you from Windows? That might help other people (including me) diagnose why you need to do this in the first place.

    0 讨论(0)
  • 2020-12-06 07:29

    Why can't you call the api

    AfxIsValidAddress((p), sizeof(type), FALSE));

    0 讨论(0)
  • 2020-12-06 07:41

    If the variable is uninitialized you are hosed. Sooner or later it's going to be an address for something you don't want to play with (like your own stack).

    If you think you need this, and (uintptr_t)var < 65536 does not suffice (Windows does not allow allocating the bottom 64k), there is no real solution. VirtualQuery, etc. appear to "work" but sooner or later will burn you.

    0 讨论(0)
提交回复
热议问题