Argument validity checking (addresses)

Georg Wittig wittig at gmdzi.UUCP
Tue Jan 23 00:40:44 AEST 1990


ggw at wolves.uucp (Gregory G. Woodbury) writes:
>When a subroutine depends on the user to pass addresses (strings, structures,
>or functions) that the subroutine is going to use, and the subroutine wants
>to be robust about not killing the process if the user makes a mistake,
>validity checking the aruments passed is one of the front line defenses.

>The problem, however, is that UN*X environments (at least Sys5 and related
>ones) do not provide a general means of determining if a given address is
>going to generate a memory fault of some kind.

My solution is the following one:

	#define MIN_NON_NIL_PTR ((unsigned long) 1L)
	#define MAX_NON_NIL_PTR ((unsigned long) 0x00ffffffL)

	if ( ! ( ((unsigned long) ptr_in_question) >= MIN_NON_NIL_PTR   &&
		 ((unsigned long) ptr_in_question) <= MAX_NON_NIL_PTR ) )
	{	... get_angry_or_whatever () ...
	}
or, if you allow a nil ptr:

	if (ptr_in_question != 0   &&   (...see above...))

I know, that's not a perfect solution. The values MIN_NON_NIL_PTR and
MAX_NON_NIL_PTR may vary from machine to machine. You know how to use #ifdef :-)
The condition ``MIN <= ptr <= MAX'' may be more complicated, and so on, and so
on ...

BUT it works on surprising number of machines.

Does someone know if there exists a portable ANSI C conforming solution for that
problem?
-- 
Georg Wittig   GMD-Z1.BI   P.O. Box 1240   D-5205 St. Augustin 1 (West Germany)
email: wittig at gmdzi.uucp   phone: (+49 2241) 14-2294
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Freedom's just another word for nothing left to lose" (Kris Kristofferson)



More information about the Comp.lang.c mailing list