Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 1 Apr 1999 09:03:32 -0500 (EST)
From:      James Snow <sno@teardrop.net>
To:        freebsd-hackers@freebsd.org
Subject:   Curiosity Killed the Array
Message-ID:  <Pine.BSF.4.05.9904010852560.36839-100000@silver.teardrop.net>

next in thread | raw e-mail | index | archive | help

In working on a C program recently, I ran into some bugs, resolved them,
and then in resolving them realized that there isn't any run-time checking
of array boundaries. 

I thought this was kind of interesting and wondered about whether or not
it could be a problem somehow. So I wrote a little C program that looks
like this:

void main( void ) {
    int array[10];
    int i = 0;
    while ( 1 == 1) {
        array[i] = i;
        print("%d\n", i);
        i++;
    }
}

to see what would happen. Much to my suprise, it increments all the way to
400 or 500 or so and then cores. (Bus error, I think.) 

I thought about this for a while and came to the conclusion that the
kernel allocates me a bit of space to work within and as long as I don't
step outside that space, it doesn't care what I'm doing. OK, that makes
sense. (To me anyway.)

So then I wondered what would happen if I changed i++; to i--;

When I ran it, it spews negative numbers as you would expect, but it just
keeps going. Watching the process in top, it started sucking up swap and
everything. It filled up the swap space on my box before the kernel jumped
in and said 'Bad!' and killed it.

So, I'm just curious as to the technical reasons behind this. (If anyone
is bored and cares to explain this to someone who's recently gotten
curious as to how the kernel does stuff.)


TIA,
-sno
          o - - - - - - - - - - - - - o - - - - - - - - - - - - o
          | We live in the short term | sno at teardrop dot org | 
          |   and hope for the best.  |  I am Geek. Hear me ^G  |
          o - - - - - - - - - - - - - o - - - - - - - - - - - - o




To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.BSF.4.05.9904010852560.36839-100000>