home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!caen!sdd.hp.com!think.com!barmar
- From: barmar@think.com (Barry Margolin)
- Newsgroups: comp.lang.c++
- Subject: Re: RUN TIME ARRAY INDEX ERRORS
- Date: 19 Nov 1992 18:16:15 GMT
- Organization: Thinking Machines Corporation, Cambridge MA, USA
- Lines: 43
- Message-ID: <1egllfINNb57@early-bird.think.com>
- References: <1992Nov19.102258.4237@alchemy.chem.utoronto.ca>
- NNTP-Posting-Host: telecaster.think.com
-
- In article <1992Nov19.102258.4237@alchemy.chem.utoronto.ca> mbersohn@alchemy.chem.utoronto.ca (M. Bersohn) writes:
- > My question to ask you is this: How did C survive all these
- >years without such an obviously essential feature? How was
- >anybody able to construct programs of 100,000 plus lines of C
- >that worked well,without this feature? I'm a naive programmer;
- >there is some magic there that escapes me.
-
- Your naivete is in assuming that all the hundreds of thousands of lines of
- C code out there work properly. Many standard Unix commands, for instance,
- will screw up badly if given too much input, for instance (there was an
- article in CACM a year or so ago that demonstrated the results of giving
- random input to various Unix commands -- it wasn't very pleasant). The
- Internet worm took advantage of a lack of bounds checking in the finger
- server (it used gets(), which doesn't have a buffer size parameter).
-
- But the practical answer to your question is that the reason most C
- implementations don't do array bounds checking is performance. And the
- reason it isn't as big a problem as it might be is that it's not really
- that hard to keep from accidentally going outside an array. You don't need
- to check before each array operation (as a simple bounds checker (such as
- one implemented by overloading operator[]) would), but only when setting
- the index variable. Often you can take advantage of invariants so that you
- know that the index is valid; for instance, in the code
-
- int my_array[ARRAY_SIZE], i;
-
- for (i = 0; i < ARRAY_SIZE; i++)
- my_array[i] = f(i);
-
- it's not necessary to do bounds checking on each assignment, since the loop
- is constructed so that it can't exceed the array bounds. A good optimizing
- compiler might be able to infer this and leave out bounds checks, but it's
- a difficult optimization to implement.
-
- Also, if you use the right library routines, (e.g. fgets() rather than
- gets(), strncpy() rather than strcpy() when copying between different-sized
- arrays) you get automatic checking there.
-
- --
- Barry Margolin
- System Manager, Thinking Machines Corp.
-
- barmar@think.com {uunet,harvard}!think!barmar
-