Most efficient replacement for IsBadReadPtr?

前端 未结 10 1269
你的背包
你的背包 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条回答
  •  Happy的楠姐
    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.

提交回复
热议问题