This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c/23846] New: suggest new warning (example: int x = x)


gcc -std=c99 -Wall gives no errors or warnings when an initialization (right 
side of =) refers to the value being initialized (left side of =).  This is 
true if the item being dereferenced has an indeterminate value, e.g., is a 
local variable:

| cat t.c
#include <stdio.h>

int main()
{
  int array[] = { 1, 2 };
  int x = 0;
  int y = array[y];

  printf("x=%d  y=%d", x, y);
}
| gcc --version
gcc (GCC) 3.3.3 (cygwin special)
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

| gcc -Wall -std=c99 -o t t.c
| ./t
x=0  y=1
|

In this case the programmer made a mistake and meant to use "array[x]" in the 
initialization of y.

I just found this in my own code - it is an easy mistake to make when doing a 
mass transformation of code.  I was lucky because the line generated a bus 
error, but this error could be insidious because the code could work in some 
environments.

My guess is that the vast majority of cases where the lvalue being initialized 
is deferenced on the right side of an initialization are mistakes.  If this is 
permitted by the standard, presumably to ease doing things like "int* x = &x", 
then maybe a new warning could be created (probably "on" by default, or part 
of "-Wall").

-- 
           Summary: suggest new warning (example: int x = x)
           Product: gcc
           Version: 3.3.1
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P1
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: ash at onezero dot org
                CC: gcc-bugs at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23846


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]