Converting FORTRAN to C

Chris Torek chris at umcp-cs.UUCP
Sat Jan 5 01:47:50 AEST 1985


The real trick to doing ``bounds checking'' in C is in figuring out
exactly what ``bounds'' really means.  Is ``p[-1]'' out of bounds?
Maybe, maybe not.  How about *(p - 1)?  It's the same thing.  p-1
is certainly not out of bounds.  &foo[bar] can never be (I claim).

How about this code fragment:

	f() {
		register char *p;
		char *g();

		p = g(10);
		p[-4] = 0;
	}

Is p[-4] out of bounds?  Depends on what g(10) returns!  (Ouch.)

If you really try hard, you could come up with runtime checks that
really worked, by having every library function and every compile
time array and every block of storage have an associated descriptor,
with rules for combining blocks and so forth.  Trouble is, you'd
wind up with something almost the same as this:

	if ((addr & 0x8000000) == 0) {
		if ((addr & 0x40000000) == 0) {
			if (addr < P0BR || addr > P0BR + P0LR) ...
		}
		else {
			if (addr < P1BR || addr > P1BR + P1LR) ...
		}
	}
	else {
		if (addr < SBR || addr > SBR + SLR) ...
	}

Look familiar?  Hm... you could even use ``segmentation fault'' and
``bus error'' to distinguish the two major cases.... :-)
-- 
(This line accidently left nonblank.)

In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (301) 454-7690
UUCP:	{seismo,allegra,brl-bmd}!umcp-cs!chris
CSNet:	chris at umcp-cs		ARPA:	chris at maryland



More information about the Comp.lang.c mailing list