Xref: utzoo comp.lang.c:25671 comp.unix.wizards:20464 Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!decwrl!nsc!taux01!arielf From: arielf@taux01.UUCP (Ariel Faigon) Newsgroups: comp.lang.c,comp.unix.wizards Subject: Re: Argument validity checking (addresses) Summary: can be done a bit better on some systems Keywords: start,end,edata,etext Message-ID: <3254@taux01.UUCP> Date: 4 Feb 90 08:13:52 GMT References: <1990Jan18.175540.12131@wolves.uucp> <1891@gmdzi.UUCP> Reply-To: arielf@taux01.UUCP (Ariel Faigon) Followup-To: comp.lang.c Organization: National Semiconductor (IC) Ltd, Israel Lines: 47 In <1990Jan26.003654.6080@NCoast.ORG> Brandon S. Allbery writes: | As quoted from <1891@gmdzi.UUCP> by wittig@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@nsc.com @{hplabs,pyramid,sun,decwrl} 34 48 E / 32 10 N