Xref: utzoo comp.lang.c:25339 comp.unix.wizards:20311 Path: utzoo!utgpu!jarvis.csri.toronto.edu!clyde.concordia.ca!uunet!mcsun!unido!gmdzi!wittig From: wittig@gmdzi.UUCP (Georg Wittig) Newsgroups: comp.lang.c,comp.unix.wizards Subject: Re: Argument validity checking (addresses) Keywords: address probing Message-ID: <1891@gmdzi.UUCP> Date: 22 Jan 90 13:40:44 GMT References: <1990Jan18.175540.12131@wolves.uucp> Organization: GMD - The German National Research Centre for Computer Science Lines: 37 ggw@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@gmdzi.uucp phone: (+49 2241) 14-2294 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Freedom's just another word for nothing left to lose" (Kris Kristofferson)