Operating System - HP-UX
1830898 Members
3460 Online
110017 Solutions
New Discussion

Trapping access to non r/w memory

 
Andrew Cunningham
Frequent Advisor

Trapping access to non r/w memory

under UX11, I want to check whether a C pointer "points to" a "legal" memory address - i.e. one probably allocated by the application, or at least in the applications memory space. Is there a system API that will tell me this?

Sort of the equivalent to the Win32 api IsBadReadPtr().
2 REPLIES 2
A. Clay Stephenson
Acclaimed Contributor

Re: Trapping access to non r/w memory

In a word, no. About the only illegal address that is easily caught is a reference to a NULL pointer value. In UNIX, system calls usually set errno to EFAULT to indicate an illegal address or in severe cases the process gets a SIGBUS or SIGILL signal. There are some 3rd-party utilities that will help like Rational's Purify but in the UNIX world, bad pointer references are considered to be the sole responsibilty of the programmer. I will concede that sometimes that programmer is the one that wrote the dumb library function that you are trying to use.

In real life, one of the most common errors is not understanding that realloc can sometimes return a new base address rather than expanding the old block of memory in place. If you have pointers which point into the original block of memory those values are no longer valid - though the contents may appear to okay until the original block is reused.
If it ain't broke, I can fix that.
Andrew Cunningham
Frequent Advisor

Re: Trapping access to non r/w memory

The reason I need to do this is that I am being passed "pointers=object references" from an external scripting interface to my C++ code. It is possible for the scripting interface ( due to programmer error) to pass me a bad pointer value. I have various guards in my internal structures, but just dereferencing the pointer to check these guards can cause an exception. Looks like I may have to catch the signal.