Spurious warning in gcc SVN trunk

Kevin P. Fleming kpfleming@digium.com
Sat Aug 1 00:41:00 GMT 2009


Just for kicks, I pulled a copy of SVN trunk today, built it, and tried
compiling some existing code. While it caught a number of problems that
previous versions did not, it also reported a couple of cases that seem
spurious. Here's a small example:

> #include <stdlib.h>
> #include <stdio.h>
> 
> int main(int argc, char **argv)
> {
>         switch (argc) {
>         case 0:
>         {
>                 char *foo = malloc(1);
>                 if (!foo)
>         TEST_LABEL:
>                 {
>                         printf("test\n");
>                         return 0;
>                 }
>                 free(foo);
>                 break;
>         }
>         case 1:
>         {
>                 char *foo = "xyz";
>                 if (argv[0]) {
>                         goto TEST_LABEL;
>                 }
>                 printf(foo);
>                 break;
>         }
>         }
>         return 0;
> }

Compiling this as follows:

> $ ~/gcc-trunk/bin/gcc -o test test.c -Wall
> test.c: In function ‘main’:
> test.c:23:4: warning: jump skips variable initialization
> test.c:11:2: note: label ‘TEST_LABEL’ defined here
> test.c:9:9: note: ‘foo’ declared here

While this warning is completely accurate, it's also spurious, because
the code that will execute after the jump to TEST_LABEL does not
reference the local variable 'foo' in the target block at all.

Now, I don't expect anyone to like this code... I've already fixed it up
to not be so horrifically bad, but it's still perfectly valid and should
not generate such a warning. Comments?

-- 
Kevin P. Fleming
Digium, Inc. | Director of Software Technologies
445 Jan Davis Drive NW - Huntsville, AL 35806 - USA
skype: kpfleming | jabber: kpfleming@digium.com
Check us out at www.digium.com & www.asterisk.org



More information about the Gcc-help mailing list