Re: Odd warning: array subscript is above array bounds

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 12/10/2017 18:42, Jeff Law wrote:
> On 10/12/2017 10:16 AM, Mason wrote:
>> Hello,
>>
>> Consider the following testcase:
>>
>> extern int n, xyz, array[8];
>> void foo(void)
>> {
>> 	int i, j, Dupe;
>>
>> 	for (i = 0; i < n; i ++) {
>> 		Dupe = 0;
>> 		for (j = 0; j < i; j ++)
>> 			if (array[j] == array[i])
>> 				Dupe = 1;
>> 		if (array[i] && !Dupe)
>> 			xyz = 42;
>> 	}
>> }
>>
>> $ gcc-7.2 -Wall -O3 -S testcase.c
>> testcase.c: In function 'foo':
>> testcase.c:11:12: warning: array subscript is above array bounds [-Warray-bounds]
>>    if (array[i] && !Dupe)
>>        ~~~~~^~~
>>
>> $ gcc-6.3 -Wall -O3 -S testcase.c
>> /* NO WARNING */
>>
>> I'm not sure how/why GCC concludes out-of-bounds access?
>> Could it be a false positive?
>
> -O3 turns on loop unrolling which often triggers false positives for the
> out-of-bounds array index checks.  I believe there's a number of bugs in
> the BZ database WRT this issue.
> 
> You might consider attaching your testcase to one of them.

I will have a look around.

In the mean time, I have made a smaller testcase:

extern int array[8];
void foo(int n, int *res)
{
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < i; ++j)
			if (array[i] == array[j])
				*res = 1;
}

$ gcc-7 -Wall -O3 -S testcase.c 
testcase.c: In function 'foo':
testcase.c:6:13: warning: array subscript is above array bounds [-Warray-bounds]
    if (array[i] == array[j])
        ~~~~~^~~
testcase.c:6:13: warning: array subscript is above array bounds [-Warray-bounds]

$ gcc-6 -Wall -O3 -S testcase.c 
testcase.c: In function 'foo':
testcase.c:6:13: warning: array subscript is above array bounds [-Warray-bounds]
    if (array[i] == array[j])
        ~~~~~^~~
testcase.c:6:13: warning: array subscript is above array bounds [-Warray-bounds]

$ gcc-5 -Wall -O3 -S testcase.c 
testcase.c: In function 'foo':
testcase.c:6:13: warning: array subscript is above array bounds [-Warray-bounds]
    if (array[i] == array[j])
             ^
testcase.c:6:13: warning: array subscript is above array bounds [-Warray-bounds]


Hmmm, this one triggers twice? And for gcc 5, 6, and 7.

gcc 5 and 6 generate the same amd64 assembly.

I'll take a closer look at the gcc 7 generated asm.

Regards.



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux