Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate LPVOID to <Bad Ptr>

Tags:

c++

I'm working with C++ unmanaged, the problem that I have happens when I call a method that returns an LPVOID.

LPVOID MyMethod(...);

The problem is that this method sometimes returns a Bad Ptr and I want to know if there is a way of detecting this, if the value returned is a Bad Ptr.

I have tried asking if it is NULL with no luck.

The only way in which I realize if the result is a Bad Ptr is while I'm debugging, I have tried some different ways but still unable to do it.

like image 239
Vic Avatar asked Dec 01 '22 12:12

Vic


1 Answers

No, there is no easy way to determine if a pointer is bad.

Windows does have IsBadReadPtr, IsBadWritePtr. These functions are inherently flawed - they only determine if a function is readable or writable in your address space at the moment of the call. They can also be the cause of security issues and should never be used.

The main issue is that there is no way to differentiate between a "bad" pointer that is still accessible to your process, and a good pointer.

For instance,

int g[5];
int somethingElse;

void* GetPointer()
{
   return &g[5]; // Whoops, off by one.
}

&g[5] is probably a valid pointer in your process, and might be pointing to somethingElse, you'll be able to access it without crashing but writing to it will corrupt your state.

like image 74
Michael Avatar answered Dec 05 '22 05:12

Michael