From owner-freebsd-hackers Thu Apr 1 6: 1: 0 1999 Delivered-To: freebsd-hackers@freebsd.org Received: from silver.teardrop.net (silver.teardrop.net [216.155.28.66]) by hub.freebsd.org (Postfix) with ESMTP id 44F2A14D12 for ; Thu, 1 Apr 1999 06:00:58 -0800 (PST) (envelope-from sno@teardrop.net) Received: from localhost (sno@localhost) by silver.teardrop.net (8.9.2/8.9.2) with ESMTP id JAA36872 for ; Thu, 1 Apr 1999 09:03:32 -0500 (EST) (envelope-from sno@teardrop.net) Date: Thu, 1 Apr 1999 09:03:32 -0500 (EST) From: James Snow To: freebsd-hackers@freebsd.org Subject: Curiosity Killed the Array Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG 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