[Bug 1249498] [NEW] gcc fails to report uninitialized local variable

Kevin Grittner kgrittn at ymail.com
Fri Nov 8 21:55:09 UTC 2013


Public bug reported:

$ lsb_release -rd
Description:    Ubuntu 12.10
Release:        12.10

$ apt-cache policy gcc
gcc:
  Installed: 4:4.7.2-1ubuntu2
  Candidate: 4:4.7.2-1ubuntu2
  Version table:
 *** 4:4.7.2-1ubuntu2 0
        500 http://us.archive.ubuntu.com/ubuntu/ quantal/main amd64 Packages
        100 /var/lib/dpkg/status


With source file named warning_test.c in the current working directory, containing this:


int warning_test(int a, int b, int c);
int warning_test(int a, int b, int c)
{
	int			result = 0;
	int			i1;
	int			i2;

	i1 = i2 = 0;
	while (i1 < 2 || i2 < 2)
	{
		int			cmpresult;

		switch (a)
		{
			case 1:
				if (b != c)
					cmpresult = (b < c) ? -1 : 1;
				break;
		}

		if (cmpresult < 0)
		{
			result = -1;
			break;
		}

		i1++, i2++;
	}

	return result;
}


... I compiled with this command line:

gcc -O2 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-
statement -Wendif-labels -Wmissing-format-attribute -Wformat-security
-fno-strict-aliasing -fwrapv -fexcess-precision=standard -g
-D_GNU_SOURCE -c -o warning_test.o warning_test.c -MMD -MP

I expected:

warning_test.c: In function ‘warning_test’:
warning_test.c:21:6: warning: ‘cmpresult’ may be used uninitialized in this function [-Wuninitialized]

I got a clean compile (no warnings).

If the reference to the i2 variable is removed from the conditions for
the while loop, the warning is generated as expected.

C source file attached in case it gets mangled above.

** Affects: gcc-defaults (Ubuntu)
     Importance: Undecided
         Status: New

** Attachment added: "C file that should get an uninitialized variable warning, but doesn't."
   https://bugs.launchpad.net/bugs/1249498/+attachment/3903556/+files/warning_test.c

-- 
You received this bug notification because you are a member of Ubuntu
Foundations Bugs, which is subscribed to gcc-defaults in Ubuntu.
https://bugs.launchpad.net/bugs/1249498

Title:
  gcc fails to report uninitialized local variable

Status in “gcc-defaults” package in Ubuntu:
  New

Bug description:
  $ lsb_release -rd
  Description:    Ubuntu 12.10
  Release:        12.10

  $ apt-cache policy gcc
  gcc:
    Installed: 4:4.7.2-1ubuntu2
    Candidate: 4:4.7.2-1ubuntu2
    Version table:
   *** 4:4.7.2-1ubuntu2 0
          500 http://us.archive.ubuntu.com/ubuntu/ quantal/main amd64 Packages
          100 /var/lib/dpkg/status

  
  With source file named warning_test.c in the current working directory, containing this:

  
  int warning_test(int a, int b, int c);
  int warning_test(int a, int b, int c)
  {
  	int			result = 0;
  	int			i1;
  	int			i2;

  	i1 = i2 = 0;
  	while (i1 < 2 || i2 < 2)
  	{
  		int			cmpresult;

  		switch (a)
  		{
  			case 1:
  				if (b != c)
  					cmpresult = (b < c) ? -1 : 1;
  				break;
  		}

  		if (cmpresult < 0)
  		{
  			result = -1;
  			break;
  		}

  		i1++, i2++;
  	}

  	return result;
  }

  
  ... I compiled with this command line:

  gcc -O2 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-
  after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-
  security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -g
  -D_GNU_SOURCE -c -o warning_test.o warning_test.c -MMD -MP

  I expected:

  warning_test.c: In function ‘warning_test’:
  warning_test.c:21:6: warning: ‘cmpresult’ may be used uninitialized in this function [-Wuninitialized]

  I got a clean compile (no warnings).

  If the reference to the i2 variable is removed from the conditions for
  the while loop, the warning is generated as expected.

  C source file attached in case it gets mangled above.

To manage notifications about this bug go to:
https://bugs.launchpad.net/ubuntu/+source/gcc-defaults/+bug/1249498/+subscriptions



More information about the foundations-bugs mailing list