I would like to check the validity of an address or pointer before I read or write to an address in a Linux executable. I'm using either C or C++. My goal is to avoid segment violation type errors if an API function is passed an invalid pointer.
Asked
Active
Viewed 181 times
1
-
1Might this be your answer? https://stackoverflow.com/questions/7134590/how-to-test-if-an-address-is-readable-in-linux-userspace-app – Super-intelligent Shade Jan 21 '15 at 23:49
-
1You could also just try to access it and catch the segfault: http://stackoverflow.com/questions/2350489/how-to-catch-segmentation-fault-in-linux – indiv Jan 21 '15 at 23:51
1 Answers
6
You just cannot. The best you can do is to check a pointer value is not a null pointer, which C terminology wise is not an invalid pointer. Even if your pointer is inside an accessible segment, it does not mean it is a valid pointer and that you will not get an undefined behavior.

ouah
- 142,963
- 15
- 272
- 331
-
@ruemorgue - one example: an address on the stack could be a valid local variable of the calling function or this function's return address from the call. In the latter case, your program is likely to fail when the function returns. – DoxyLover Jan 22 '15 at 05:14