Argument validity checking (addresses)

Ariel Faigon arielf at taux01.UUCP
Sun Feb 4 19:13:52 AEST 1990


In <1990Jan26.003654.6080 at NCoast.ORG> Brandon S. Allbery writes:
| As quoted from <1891 at gmdzi.UUCP> by wittig at gmdzi.UUCP (Georg Wittig):
| +---------------
| | My solution is the following one:
| | 
| | 	#define MIN_NON_NIL_PTR ((unsigned long) 1L)
| | 	#define MAX_NON_NIL_PTR ((unsigned long) 0x00ffffffL)
| +---------------
|
I liked Brandon's original suggestion (to pass the address to some
system-call which checks for EFAULT).

Anyway, without claiming that the following solution is portable/general/
whatever I'll post my contribution to this thread,
just because on some systems it may be a bit better than Georg's solution
(although basically the same idea).

Quoted from some derivative of a 4.x BSD manual on end(3):

NAME
     end, etext, edata - last locations in program

SYNOPSIS
     extern end;
     extern etext;
     extern edata;

So (I add 'start' which may be defined in your C startup module):

#define IN_MY_TEXT(addr) ((void *) &start <= (addr) < (void *) &etext)
#define IN_MY_DATA(addr) (!(IN_MY_TEXT(addr) && (addr) < (void *) &end)
#define IN_MY_HEAP(addr) ((void *) &end <= (addr) < (void *) sbrk(0))
#define IN_MY_ADDRESS_SPACE(addr) \
	(IN_MY_TEXT(addr) || IN_MY_DATA(addr) || IN_MY_HEAP(addr))

(disclaimer: this code wasn't tested).

This still doesn't handle gaps, shared memory segments, and stack space
you can check (again, not bullet-proof) for an address near the top of
your stack by comparing 'addr' to some local variable address.

Just another approximation for the truth :-)
-- 
Ariel Faigon, CTP group, NSTA
National Semiconductor (Israel)
6 Maskit st.  P.O.B. 3007, Herzlia 46104, Israel   Tel. (972)52-522312
arielf%taux01 at nsc.com   @{hplabs,pyramid,sun,decwrl} 34 48 E / 32 10 N



More information about the Comp.unix.wizards mailing list