The following test case produces a 'may be used uninitialized' warning that
refers to a variable that isn't in scope at the point of the warning:

> cat nntpinit.c
struct hostent {
    char **h_addr_list;
};
struct hostent *gethostbyname(const char*);
int socket(void);
int close(int);
int connect(int, const char*);
void foo(void);

static int get_tcp_socket(const char *machine)
{
    struct hostent *hp;
    int s42, x;
    char **addr;

    hp = gethostbyname(machine);
    x = 0;
    for (addr = hp->h_addr_list; *addr; addr++) {
        s42 = socket();
        if (s42 < 0)
            return -1;
        x = connect(s42, *addr);
        if (x == 0)
            break;
        close(s42);
    }
    if (x < 0)
        return -1;
    return s42;
}

int server_init(const char *machine)
{
    int sockt_rd;

    sockt_rd = get_tcp_socket(machine);
    foo();
    if (sockt_rd < 0)
        return -1;
    return 0;
}
> gcc -O2 -Wall -c nntpinit.c
nntpinit.c: In function 'server_init':
nntpinit.c:38: warning: 's42' may be used uninitialized in this function

There is indeed a 'may be used uninitialized' issue in this code, but it's
actually in get_tcp_socket(), not in server_init() because there every use is
trivially preceeded by a def.

I guess that automatic inlining is messing up name and context information.

The test case is distilled down from some ancient nntp client code I'm tidying
up, and the bogus data in the warning did cause some headscratching before the
warning could be analysed and fixed (set x = -1 in get_tcp_socket()).


-- 
           Summary: bogus name and location in 'may be used uninitialized'
                    warning
           Product: gcc
           Version: 4.4.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: mikpe at it dot uu dot se
  GCC host triplet: i686-pc-linux-gnu


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

Reply via email to