🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.gcc.bug
Article #6252

Spurious "uninitialized variable" warning from gcc

#6252
From: Adam Wagman
Date: Wed, 27 Dec 1995 00:00
90 lines
2758 bytes
    I've been working on some code, and gcc gives an incorrect warning
that a variable may be used uninitialized.  I don't know if this is
considered a bug, or if it's just that handling the general problem is
somewhere in the difficult-to-impossible regime.  Example code is at the
end of this message, along with compiler options.  The gcc version is 
  Reading specs from /usr/gnu/lib/gcc-lib/sparc-sun-sunos4.1.3/2.6-95q1/specs
  gcc version 2.6-95q1
    Basically, the problem is that there is a code path in which a 
variable is uninitialized, and one where the variable is used, and the 
paths clearly do not "mix."  I encountered this with a homebrew catch-and-
throw mechanism (with the throw having the "noreturn" attribute), but
the spurious warning occurs with ordinary functions as well. 

      int n, sig;
      if (( sig = my_catch(0)) != 0)
          goto bottom;

      n = 5;
  bottom:
      if ( sig != 0 )
          my_throw(sig);  /* does not return */
      return n;

Since the initialization is skipped if and only if sig is nonzero, and since
the noreturn function is executed if sig is nonzero, the variable n can never
be used uninitialized.

I have included two different examples, because on at least one machine the
first example does not always get a warning; I've assumed that gcc was
actually optimizing the problem away in that case. 
 
Thanks for you time,
   Adam Wagman
----------------------
#include <stddef.h>
#include <stdlib.h>

typedef struct node
{
    int count;
    struct node * next_p;
} node;

#ifndef __GNUC__
#error
#endif

int my_catch(int sig);
void my_throw(int sig) __attribute__((noreturn));

int catch_test(int x);
void f(int * num);

int catch_test(int x)
{
    int n, sig;
    
    if ((sig = my_catch(0)) != 0)
	goto bottom;

    n = x;

bottom:
    if (sig)
	my_throw(sig);
    return n;
}


void f(int * num)
{
  node *p = NULL, *pl;
  int sig, n;

  if ((sig = my_catch (0)) != 0) goto hell;

  for (n = 0, pl = p;pl; pl = pl->next_p)
    n += pl->count;

hell:
  /* On one machine, the spurious warning occurs if and only if the
     following line is left in. */
#if 1
  if (p) free(p), p = NULL;
#endif
  if (sig)
      my_throw (sig);
  *num = n;
  return;
}
Technical Details
Message-ID: <199512271611.LAA18726@elephant_walk.cognex.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 113d02,bc645508492a1aa0,start
X-Google-Attributes: gid113d02,public
From: wagman@cognex.COM (Adam Wagman)
Subject: Spurious "uninitialized variable" warning from gcc
Date: 1995/12/27
Message-ID: <199512271611.LAA18726@elephant_walk.cognex.com>
X-Deja-AN: 122960359
distribution: gnu
sender: daemon@cis.ohio-state.edu
organization: GNUs Not Usenet
newsgroups: gnu.gcc.bug