|
From: John B. <joh...@ho...> - 2012-12-18 16:11:30
|
Gisle Vanem wrote:
>
> I'm sorry if issues with MingW64-gcc is off-topic here. But I'm having
> trouble understanding the following warning when using '-O2'. This minimal
> program:
>
> #include <stdlib.h>
> #define MAX_PROTO_ALIASES 0
> struct _protoent {
> char *p_name;
> char *p_aliases [MAX_PROTO_ALIASES+1];
> int p_proto;
> struct _protoent *p_next;
> };
>
> extern struct _protoent *proto0;
>
> void endprotoent (void)
> {
> struct _protoent *p, *next;
>
> for (p = proto0; p; p = next)
> {
> int i;
> for (i = 0; p->p_aliases[i]; i++) < < line 20
> free (p->p_aliases[i]);
> next = p->p_next;
> free (p->p_name);
> free (p);
> }
> }
>
> produces this warning:
>
> x86_64-w64-mingw32-gcc -O2 -c -m64 -Wall array-bounds.c
> array-bounds.c: In function 'endprotoent':
> array-bounds.c:20:29: warning: array subscript is above array bounds [-Warray-bounds]
>
> What is this all about? How does gcc believe 'i' can get above 1' ?
> This warning isn't produced with the "normal" mingw gcc 4.7.2.
>
> ---------
In my opinion, the compiler can only be sure of the maximum value of i if the loop
were written:
for (i = 0; i < 1; i++) // or maybe i < sizeof (p->p_aliases / sizeof(char *)
{
// blah-blah-blah
}
As written, your loop will continue as long as p->p_aliases[i] != 0. This imposes
no *compile-time* upper bound on i, so you *should* get a warning.. Of course,
at run-time you will exceed your array bounds and suffer the consequences dictated
by the way RAM is allocated to your program.
Regards,
John Brown.
|