Bug 52167 - self-initialization should at least produce use-of-uninitialized warning
Summary: self-initialization should at least produce use-of-uninitialized warning
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 4.4.5
: P3 enhancement
Target Milestone: 11.0
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
: 53287 70991 82900 (view as bug list)
Depends on: 48829 48483
Blocks: Wuninitialized
  Show dependency treegraph
 
Reported: 2012-02-08 08:24 UTC by Darko Veberic
Modified: 2021-03-25 22:25 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 10.2.0, 4.1.0, 4.8.4, 4.9.4, 5.5.0, 6.4.0, 7.2.0, 8.3.0, 9.1.0
Last reconfirmed: 2012-02-08 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Darko Veberic 2012-02-08 08:24:18 UTC
the following code gets compiled without any warnings (even with -Wall -Wextra):

std::string foo(foo);

and the resulting code segfaults (clang++ is also silent on this but the code throws std::length_error).

i am aware that the example is ridiculous but it comes from a large real-life project and is possibly a result of negligent copy-paste operations, so at least issuing a warning would be nice.

full example:


#include <iostream>
#include <string>
using namespace std;

int
main()
{
  const string foo(foo);
  cout << foo << endl;
  return 0;
}
Comment 1 Jonathan Wakely 2012-02-08 09:03:41 UTC
You need -Winit-self, but it doesn't work for class types.
Comment 2 Manuel López-Ibáñez 2012-02-08 14:01:05 UTC
Clang++ 3.0 warns:

/tmp/webcompile/_14716_2.cc:8:20: warning: variable 'foo' is uninitialized when used within its own initialization [-Wuninitialized]
  const string foo(foo);
               ~~~ ^~~
1 warning generated.

Clang improves a lot every 6 months, you should always check the latest version (or SVN if possible).
Comment 3 Andrew Pinski 2012-02-09 07:42:33 UTC
Related to PR 48829 and PR 48483.
Comment 4 Manuel López-Ibáñez 2012-10-25 09:15:35 UTC
*** Bug 53287 has been marked as a duplicate of this bug. ***
Comment 5 Manuel López-Ibáñez 2017-03-03 23:38:59 UTC
*** Bug 70991 has been marked as a duplicate of this bug. ***
Comment 6 Martin Sebor 2017-11-08 16:34:43 UTC
*** Bug 82900 has been marked as a duplicate of this bug. ***
Comment 7 Martin Sebor 2017-11-08 16:40:07 UTC
Let me adjust the Summary to better reflect the request.
Comment 8 Martin Sebor 2021-03-25 22:25:59 UTC
GCC 11 (since g:b825a22890740f341eae566af27e18e528cd29a7) diagnoses passing an uninitialized object by const reference by -Wmaybe-uninitialized:

$ g++ -S -Wall pr52167.C
pr52167.C: In function ‘int main()’:
pr52167.C:8:23: warning: ‘foo’ may be used uninitialized [-Wmaybe-uninitialized]
    8 |   const string foo(foo);
      |                       ^
In file included from /build/gcc-master/x86_64-pc-linux-gnu/libstdc++-v3/include/string:55,
                 from /build/gcc-master/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/locale_classes.h:40,
                 from /build/gcc-master/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/ios_base.h:41,
                 from /build/gcc-master/x86_64-pc-linux-gnu/libstdc++-v3/include/ios:42,
                 from /build/gcc-master/x86_64-pc-linux-gnu/libstdc++-v3/include/ostream:38,
                 from /build/gcc-master/x86_64-pc-linux-gnu/libstdc++-v3/include/iostream:39,
                 from pr52167.C:1:
/build/gcc-master/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/basic_string.h:448:7: note: by argument 2 of type ‘const std::__cxx11::basic_string<char>&’ to ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ declared here
  448 |       basic_string(const basic_string& __str)
      |       ^~~~~~~~~~~~
pr52167.C:8:16: note: ‘foo’ declared here
    8 |   const string foo(foo);
      |                ^~~