Path: utzoo!utgpu!watmath!rbutterworth From: rbutterworth@watmath.waterloo.edu (Ray Butterworth) Newsgroups: comp.lang.c Subject: Re: detecting invalid pointers Message-ID: <24230@watmath.waterloo.edu> Date: 14 Mar 89 16:33:14 GMT References: <15495@cup.portal.com> <11998@haddock.ima.isc.com> <767@twwells.uucp> Organization: U of Waterloo, Ontario Lines: 32 In article <767@twwells.uucp>, bill@twwells.uucp (T. William Wells) writes: > In article <3011@nunki.usc.edu> jeenglis@nunki.usc.edu (Joe English) writes: > : In My Opinion, you should never *have* to check a raw pointer for > : validity. Any code that might possibly generate an out-of-range > : pointer should check the subscript (or loop count, or whatever) > : beforehand. I wouldn't bother to validate pointers inside, say, a > : utility routine either (other than checking for non-NULL), because it > : takes space and time and, quite frankly, it's the caller's > : responsibility not to pass bad pointers around. > > One might want to check pointer validity to cope with program > behavior that is outside the C model. Such can result in an invalid > pointer even when all other pointers are valid. Consider a pointer > munged by a bad array reference. There is a common case where the pointers are perfectly valid and yet you still need to check to see if one points inside the other even though they might actually point to completely different objects. Consider implementing memmov(void *to, void *from, size_t bytes). You have to determine if the arrays starting at "to" and at "from" overlap before you know which direction is safe for doing the copy. i.e. if "from" points inside the "to" array, you start the copy at the beginning of the array; but if "to" points inside the "from" array, you start the copy at the end of the array. (If neither condition is true it doesn't matter where you start the copy, and if both conditions are true you don't need to do the copy). As has been discussed in recent postings, there isn't any obvious simple way of performing these tests required by the pANS C library.