public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/96121] New: Uninitialized variable copying not diagnosed
@ 2020-07-08 16:10 antoshkka at gmail dot com
2020-07-08 16:12 ` [Bug c++/96121] " redi at gcc dot gnu.org
` (9 more replies)
0 siblings, 10 replies; 11+ messages in thread
From: antoshkka at gmail dot com @ 2020-07-08 16:10 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96121
Bug ID: 96121
Summary: Uninitialized variable copying not diagnosed
Product: gcc
Version: 11.0
Status: UNCONFIRMED
Keywords: diagnostic
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: antoshkka at gmail dot com
Target Milestone: ---
Consider the example:
struct A { A(); };
struct B { B(A); };
struct composed2 {
B b_;
A a_;
composed2() : b_(a_) {}
};
GCC does not diagnose the uninitialized variable `a_` usage with -Wall and
-Wextra. Some other compiler do diagnose:
warning: field 'a_' is uninitialized when used here [-Wuninitialized]
composed2() : b_(a_) {}
^
Godbolt playground: https://godbolt.org/z/AbqzjR
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/96121] Uninitialized variable copying not diagnosed
2020-07-08 16:10 [Bug c++/96121] New: Uninitialized variable copying not diagnosed antoshkka at gmail dot com
@ 2020-07-08 16:12 ` redi at gcc dot gnu.org
2020-07-08 16:24 ` glisse at gcc dot gnu.org
` (8 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: redi at gcc dot gnu.org @ 2020-07-08 16:12 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96121
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Ever confirmed|0 |1
Status|UNCONFIRMED |NEW
Last reconfirmed| |2020-07-08
--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
This is probably a dup of one of the many bugs about missing -Wuninitialized
diagnostics in mem-initializers.
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/96121] Uninitialized variable copying not diagnosed
2020-07-08 16:10 [Bug c++/96121] New: Uninitialized variable copying not diagnosed antoshkka at gmail dot com
2020-07-08 16:12 ` [Bug c++/96121] " redi at gcc dot gnu.org
@ 2020-07-08 16:24 ` glisse at gcc dot gnu.org
2020-07-08 16:27 ` glisse at gcc dot gnu.org
` (7 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: glisse at gcc dot gnu.org @ 2020-07-08 16:24 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96121
--- Comment #2 from Marc Glisse <glisse at gcc dot gnu.org> ---
gcc warns for this at the level of actual instructions, not user code. Since A
is empty, nothing uninitialized is getting copied.
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/96121] Uninitialized variable copying not diagnosed
2020-07-08 16:10 [Bug c++/96121] New: Uninitialized variable copying not diagnosed antoshkka at gmail dot com
2020-07-08 16:12 ` [Bug c++/96121] " redi at gcc dot gnu.org
2020-07-08 16:24 ` glisse at gcc dot gnu.org
@ 2020-07-08 16:27 ` glisse at gcc dot gnu.org
2020-07-08 16:29 ` antoshkka at gmail dot com
` (6 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: glisse at gcc dot gnu.org @ 2020-07-08 16:27 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96121
--- Comment #3 from Marc Glisse <glisse at gcc dot gnu.org> ---
And this translation unit doesn't actually generate any code at all, so the way
the warning is currently implemented has no chance of even looking at it.
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/96121] Uninitialized variable copying not diagnosed
2020-07-08 16:10 [Bug c++/96121] New: Uninitialized variable copying not diagnosed antoshkka at gmail dot com
` (2 preceding siblings ...)
2020-07-08 16:27 ` glisse at gcc dot gnu.org
@ 2020-07-08 16:29 ` antoshkka at gmail dot com
2020-07-08 16:55 ` glisse at gcc dot gnu.org
` (5 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: antoshkka at gmail dot com @ 2020-07-08 16:29 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96121
--- Comment #4 from Antony Polukhin <antoshkka at gmail dot com> ---
Adding members and usage does not make a difference
https://godbolt.org/z/VommHu
struct A {
A();
int i;
};
struct B {
B(A);
int i;
};
struct composed2 {
B b_;
A a_;
composed2() : b_(a_) {}
};
auto test() {
return composed2{};
}
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/96121] Uninitialized variable copying not diagnosed
2020-07-08 16:10 [Bug c++/96121] New: Uninitialized variable copying not diagnosed antoshkka at gmail dot com
` (3 preceding siblings ...)
2020-07-08 16:29 ` antoshkka at gmail dot com
@ 2020-07-08 16:55 ` glisse at gcc dot gnu.org
2020-07-13 12:08 ` manu at gcc dot gnu.org
` (4 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: glisse at gcc dot gnu.org @ 2020-07-08 16:55 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96121
--- Comment #5 from Marc Glisse <glisse at gcc dot gnu.org> ---
Yes, then we are back to the fact that it works for A=int but not for A a class
containing an int.
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/96121] Uninitialized variable copying not diagnosed
2020-07-08 16:10 [Bug c++/96121] New: Uninitialized variable copying not diagnosed antoshkka at gmail dot com
` (4 preceding siblings ...)
2020-07-08 16:55 ` glisse at gcc dot gnu.org
@ 2020-07-13 12:08 ` manu at gcc dot gnu.org
2020-11-13 3:07 ` [Bug c++/96121] Uninitialized variable copying in member initialized list " mpolacek at gcc dot gnu.org
` (3 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: manu at gcc dot gnu.org @ 2020-07-13 12:08 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96121
Manuel López-Ibáñez <manu at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Depends on| |19808
CC| |manu at gcc dot gnu.org
Keywords| |easyhack
--- Comment #6 from Manuel López-Ibáñez <manu at gcc dot gnu.org> ---
Probably a dup of bug 19808. There is an unfinished patch there that does the
check in the FE. That would side-step issues with A being empty (you just need
to check the order in which things are initialized in the mem-list).
Referenced Bugs:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=19808
[Bug 19808] miss a warning about uninitialized member usage in member
initializer list in constructor
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/96121] Uninitialized variable copying in member initialized list not diagnosed
2020-07-08 16:10 [Bug c++/96121] New: Uninitialized variable copying not diagnosed antoshkka at gmail dot com
` (5 preceding siblings ...)
2020-07-13 12:08 ` manu at gcc dot gnu.org
@ 2020-11-13 3:07 ` mpolacek at gcc dot gnu.org
2021-11-19 3:38 ` cvs-commit at gcc dot gnu.org
` (2 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-11-13 3:07 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96121
Marek Polacek <mpolacek at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |mpolacek at gcc dot gnu.org
Assignee|unassigned at gcc dot gnu.org |mpolacek at gcc dot gnu.org
Status|NEW |ASSIGNED
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/96121] Uninitialized variable copying in member initialized list not diagnosed
2020-07-08 16:10 [Bug c++/96121] New: Uninitialized variable copying not diagnosed antoshkka at gmail dot com
` (6 preceding siblings ...)
2020-11-13 3:07 ` [Bug c++/96121] Uninitialized variable copying in member initialized list " mpolacek at gcc dot gnu.org
@ 2021-11-19 3:38 ` cvs-commit at gcc dot gnu.org
2021-11-19 3:54 ` mpolacek at gcc dot gnu.org
2021-11-19 3:54 ` mpolacek at gcc dot gnu.org
9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-11-19 3:38 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96121
--- Comment #7 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The trunk branch has been updated by Marek Polacek <mpolacek@gcc.gnu.org>:
https://gcc.gnu.org/g:0790c8aacdfb4fd096aa580dae0fe49172c43ab2
commit r12-5391-g0790c8aacdfb4fd096aa580dae0fe49172c43ab2
Author: Marek Polacek <polacek@redhat.com>
Date: Tue Nov 10 20:07:24 2020 -0500
c++: Implement -Wuninitialized for mem-initializers (redux) [PR19808]
2021 update: Last year I posted a version of this patch:
<https://gcc.gnu.org/pipermail/gcc-patches/2020-November/559162.html>
but it didn't make it in. The main objection seemed to be that the
patch tried to do too much, and overlapped with the ME uninitialized
warnings. Since the patch used walk_tree without any data flow info,
it issued false positives for things like a(0 ? b : 42) and similar.
I'll admit I've been dreading resurrecting this because of the lack
of clarity about where we should warn about what. On the other hand,
I think we really should do something about this. So I've simplified
the original patch as much as it seemed reasonable. For instance, it
doesn't even attempt to handle cases like "a((b = 42)), c(b)" -- for
these I simply give up for the whole mem-initializer (but who writes
code like that, anyway?). I also give up when a member is initialized
with a function call, because we don't know what the call could do.
See Wuninitialized-17.C, for which clang emits a false positive but
we don't. I remember having a hard time dealing with initializer lists
in my previous patch, so now I only handle simple a{b} cases, but no
more. It turned out that this abridged version still warns about 90%
cases where users would expect a warning.
More complicated cases are left for the ME, which, for unused inline
functions, will only warn with -fkeep-inline-functions, but so be it.
(This is bug 21678.)
This patch implements the long-desired -Wuninitialized warning for
member initializer lists, so that the front end can detect bugs like
struct A {
int a;
int b;
A() : b(1), a(b) { }
};
where the field 'b' is used uninitialized because the order of member
initializers in the member initializer list is irrelevant; what matters
is the order of declarations in the class definition.
I've implemented this by keeping a hash set holding fields that are not
initialized yet, so at first it will be {a, b}, and after initializing
'a' it will be {b} and so on. Then I use walk_tree to walk the
initializer and if we see that an uninitialized object is used, we warn.
Of course, when we use the address of the object, we may not warn:
struct B {
int &r;
int *p;
int a;
B() : r(a), p(&a), a(1) { } // ok
};
Likewise, don't warn in unevaluated contexts such as sizeof. Classes
without an explicit initializer may still be initialized by their
default constructors; whether or not something is considered initialized
is handled in perform_member_init, see member_initialized_p.
PR c++/19808
PR c++/96121
gcc/cp/ChangeLog:
* init.c (perform_member_init): Remove a forward declaration.
Walk the initializer using find_uninit_fields_r. New parameter
to track uninitialized fields. If a member is initialized,
remove it from the hash set.
(perform_target_ctor): Return the initializer.
(struct find_uninit_data): New class.
(find_uninit_fields_r): New function.
(find_uninit_fields): New function.
(emit_mem_initializers): Keep and initialize a set holding fields
that are not initialized. When handling delegating constructors,
walk the constructor tree using find_uninit_fields_r. Also when
initializing base clases. Pass uninitialized down to
perform_member_init.
gcc/ChangeLog:
* doc/invoke.texi: Update documentation for -Wuninitialized.
* tree.c (stabilize_reference): Set location.
gcc/testsuite/ChangeLog:
* g++.dg/warn/Wuninitialized-14.C: New test.
* g++.dg/warn/Wuninitialized-15.C: New test.
* g++.dg/warn/Wuninitialized-16.C: New test.
* g++.dg/warn/Wuninitialized-17.C: New test.
* g++.dg/warn/Wuninitialized-18.C: New test.
* g++.dg/warn/Wuninitialized-19.C: New test.
* g++.dg/warn/Wuninitialized-20.C: New test.
* g++.dg/warn/Wuninitialized-21.C: New test.
* g++.dg/warn/Wuninitialized-22.C: New test.
* g++.dg/warn/Wuninitialized-23.C: New test.
* g++.dg/warn/Wuninitialized-24.C: New test.
* g++.dg/warn/Wuninitialized-25.C: New test.
* g++.dg/warn/Wuninitialized-26.C: New test.
* g++.dg/warn/Wuninitialized-27.C: New test.
* g++.dg/warn/Wuninitialized-28.C: New test.
* g++.dg/warn/Wuninitialized-29.C: New test.
* g++.dg/warn/Wuninitialized-30.C: New test.
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/96121] Uninitialized variable copying in member initialized list not diagnosed
2020-07-08 16:10 [Bug c++/96121] New: Uninitialized variable copying not diagnosed antoshkka at gmail dot com
` (7 preceding siblings ...)
2021-11-19 3:38 ` cvs-commit at gcc dot gnu.org
@ 2021-11-19 3:54 ` mpolacek at gcc dot gnu.org
2021-11-19 3:54 ` mpolacek at gcc dot gnu.org
9 siblings, 0 replies; 11+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2021-11-19 3:54 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96121
Bug 96121 depends on bug 19808, which changed state.
Bug 19808 Summary: miss a warning about uninitialized member usage in member initializer list in constructor
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=19808
What |Removed |Added
----------------------------------------------------------------------------
Status|ASSIGNED |RESOLVED
Resolution|--- |FIXED
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/96121] Uninitialized variable copying in member initialized list not diagnosed
2020-07-08 16:10 [Bug c++/96121] New: Uninitialized variable copying not diagnosed antoshkka at gmail dot com
` (8 preceding siblings ...)
2021-11-19 3:54 ` mpolacek at gcc dot gnu.org
@ 2021-11-19 3:54 ` mpolacek at gcc dot gnu.org
9 siblings, 0 replies; 11+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2021-11-19 3:54 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96121
Marek Polacek <mpolacek at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|ASSIGNED |RESOLVED
Resolution|--- |FIXED
--- Comment #8 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
At last, implemented.
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2021-11-19 3:54 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-08 16:10 [Bug c++/96121] New: Uninitialized variable copying not diagnosed antoshkka at gmail dot com
2020-07-08 16:12 ` [Bug c++/96121] " redi at gcc dot gnu.org
2020-07-08 16:24 ` glisse at gcc dot gnu.org
2020-07-08 16:27 ` glisse at gcc dot gnu.org
2020-07-08 16:29 ` antoshkka at gmail dot com
2020-07-08 16:55 ` glisse at gcc dot gnu.org
2020-07-13 12:08 ` manu at gcc dot gnu.org
2020-11-13 3:07 ` [Bug c++/96121] Uninitialized variable copying in member initialized list " mpolacek at gcc dot gnu.org
2021-11-19 3:38 ` cvs-commit at gcc dot gnu.org
2021-11-19 3:54 ` mpolacek at gcc dot gnu.org
2021-11-19 3:54 ` mpolacek at gcc dot gnu.org
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).