Bug 81674 - gcc cannot detect missing initialisers for fields in constructors
Summary: gcc cannot detect missing initialisers for fields in constructors
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 7.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic, patch
Depends on:
Blocks: Wuninitialized cppcheck
  Show dependency treegraph
 
Reported: 2017-08-02 14:36 UTC by David Binderman
Modified: 2024-04-10 10:42 UTC (History)
6 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-04-02 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description David Binderman 2017-08-02 14:36:36 UTC
Given the following C++ source code:

struct S
{
	int a, b, c;

	S() : a( 0)
	{
		b = 1;
	};
};

then I cannot get gcc to detect the missing initialiser for field c.

$ ~/gcc/results/bin/gcc -c -O2 -Wall -Wextra aug2a.cc
$

Here is static analyser cppcheck finding the problem:

$ ~/cppcheck/trunk/cppcheck --enable=all aug2a.cc
[aug2a.cc:8]: (warning) Member variable 'S::c' is not initialized in the constructor.
$

cppcheck can find 55 examples of this problem in the gcc source code alone,
so it would appear to be of some value to gcc, to say nothing of customers
of gcc.
Comment 1 Jonathan Wakely 2017-08-04 20:08:41 UTC
There are several bugs about this already, please search for duplicates.
Comment 2 Martin Sebor 2018-02-02 00:22:38 UTC
Bug 19808 is similar but not quite the same.  It (and its duplicates) asks for a warning when an uninitialized member is used to initialized another.  This one seems to be asking for a warning for members that are left uninitialized, even when they are not used.  This would be a noisier warning that -Wuninitialized so it would probably need its own option, but I think it would useful nonetheless.

It's likely that there already is a request like this one but I can't find it.  Let me confirm this in the meantime.
Comment 3 Eric Gallager 2018-02-06 15:29:43 UTC
-Weffc++ catches it:

$ $ /usr/local/bin/g++ -c -O2 -Wall -Wextra -pedantic -Weffc++ 81674.cc
81674.cc: In constructor 'S::S()':
81674.cc:5:3: warning: 'S::b' should be initialized in the member initialization list [-Weffc++]
   S() : a( 0)
   ^
81674.cc:5:3: warning: 'S::c' should be initialized in the member initialization list [-Weffc++]
$
Comment 4 Jonathan Wakely 2018-02-06 15:59:24 UTC
But -Weffc++ also warns about members that don't need to be initialized, so is useless in detecting uninitialized data.
Comment 5 Manuel López-Ibáñez 2018-09-20 22:04:36 UTC
The patch here: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=19808#c29
once finished should be able to catch this, because it walks the mem-initializer list and marks whatever is initialized. At the end of the list, it could just warn about whatever was not initialized.

There will be false positives because it will not look at the body of the constructor but this is something that could be improved later and, in any case, it will never be fixed because it is as hard as -Wuninitialized.
Comment 6 Martin Sebor 2021-04-02 16:57:50 UTC
Reconfirmed with GCC 11.  See also pr78391.  The patch in pr19808 comment 29 was either never committed or even submitted, and the other patch for the same bug submitted last November (https://gcc.gnu.org/pipermail/gcc-patches/2020-November/559162.html) wasn't approved.  Let me CC Marek to see if plans to resubmit it for GCC 12.
Comment 7 Eric Gallager 2021-11-19 16:21:32 UTC
(In reply to Martin Sebor from comment #6)
> Reconfirmed with GCC 11.  See also pr78391.  The patch in pr19808 comment 29
> was either never committed or even submitted, and the other patch for the
> same bug submitted last November
> (https://gcc.gnu.org/pipermail/gcc-patches/2020-November/559162.html) wasn't
> approved.  Let me CC Marek to see if plans to resubmit it for GCC 12.

the patch for pr19808 has been committed now