Out of range pointers

Barry Margolin barmar at think.COM
Thu Sep 22 05:00:21 AEST 1988


In article <33547 at XAIT.XEROX.COM> g-rh at XAIT.Xerox.COM (Richard Harter) writes:
>However it would be very nice if there were a library routine that would
>tell you whether a pointer was legal or not.

One problem with this is that on the segmented machines it is the act
of computing such a pointer that is invalid, not the pointer itself.
For example, if P is a pointer that happens to point to offset 0 in a
segment, *computing* P-1 will cause a fault.  So, what you need is a
routine that tells you whether a particular offset from a pointer is
legal; something like:

	if (valid_pointer_offset (P, sizeof(*P), -1))
	    P--;

Also, in order for this to work on machines with linear address
spaces, all pointers would have to carry around the location and size
of the objects to which they point.  This is done in Symbolics C,
since pointers are actually implemented as two Lisp objects, an array
and an offset into the array, and array objects contain their size,
which is checked by the microcode/hardware array-indexing operations.
Each call to malloc returns a new array, and each stack frame can be
treated as an array (this means that it won't detect overflowing from
one local array into another in the same frame, but nothing's
perfect).  Expecting C implementations on conventional architectures
to do this is too much.

Barry Margolin
Thinking Machines Corp.

barmar at think.com
{uunet,harvard}!think!barmar



More information about the Comp.lang.c mailing list