c/8268: no compile time array index checking

d.binderman@virgin.net d.binderman@virgin.net
Thu Oct 17 14:26:00 GMT 2002


>Number:         8268
>Category:       c
>Synopsis:       no compile time array index checking
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    unassigned
>State:          open
>Class:          accepts-illegal
>Submitter-Id:   net
>Arrival-Date:   Thu Oct 17 14:26:00 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator:     d.binderman@virgin.net
>Release:        gcc 3.2
>Organization:
>Environment:

>Description:
# include <stdlib.h>

// some compilers can find fault with this

// simple case

int a[ 10];

void
f()
{
	a[ -1] = -1;	// bug
	a[ 0] = 0;
	a[ 9] = 9;
	a[ 10] = 10;	// bug
}

// bit more complex

void
g()
{
	int b[ 10];

	const int n = 5;

	b[ 2 * n - 11] = -1;	// bug
	b[ 2 * n - 10] = 0;
	b[ n + 4] = 9;
	b[ n + 5] = 10;		// bug
}

// Can any compiler find fault with this ?

void
h()
{
	int * const c = (int *) malloc( 10 * sizeof( int));

	int n = 5;

	c[ 2 * n - 11] = -1;	// bug
	c[ 2 * n - 10] = 0;
	c[ n + 4] = 9;
	c[ n + 5] = 10;			// bug

	free( c);
}
>How-To-Repeat:
I tried to compile the above source code with
gcc 3.2 -g -O2 -Wall. 

It produced no warnings. I count six bugs in the code.
>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:



More information about the Gcc-bugs mailing list