From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id C2832385842B; Tue, 10 Aug 2021 19:42:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C2832385842B From: "redi at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/101850] Initialising a struct/class variable to itself does not fail at compile time (but throws std::bad_alloc at run time, as expected) Date: Tue, 10 Aug 2021 19:42:30 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 8.4.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: redi at gcc dot gnu.org X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 10 Aug 2021 19:42:30 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D101850 --- Comment #3 from Jonathan Wakely --- (In reply to Prasanta Behera from comment #0) > Version info: > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > ~$ g++ --version > g++ (Ubuntu 8.4.0-1ubuntu1~18.04) 8.4.0 > Copyright (C) 2018 Free Software Foundation, Inc. > This is free software; see the source for copying conditions. There is NO > warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOS= E. >=20 > ~$ gcc --version > gcc (Ubuntu 8.4.0-1ubuntu1~18.04) 8.4.0 > Copyright (C) 2018 Free Software Foundation, Inc. > This is free software; see the source for copying conditions. There is NO > warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOS= E. >=20 > Sample Program: > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > ~$ cat t.cpp > #include >=20 > int main() { > std::string s =3D s; > return 0; > } >=20 > Problem Description: > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > The above program tries to initialise a string with itself which is wrong! Yes, it's undefined behaviour. > However g++ does not show any compile time error, but the resulting binary > fails at run time throwing std::bad_alloc as shown below. That's one way that undefined behaviour can manifest itself. You cannot exp= ect to get a compile time error for all cases of undefined behaviour, that's why it's undefined. > ~$ g++ t.cpp > ~$ ./a.out > terminate called after throwing an instance of 'std::bad_alloc' > what(): std::bad_alloc > Aborted (core dumped) >=20 > gcc however shows a link time error which is slightly better. >=20 > ~$ gcc t.cpp > /tmp/cc30YkgH.o: In function `main': > t.cpp:(.text+0x27): undefined reference to `std::__cxx11::basic_string std::char_traits, std::allocator > >::basic_string(std::__cxx11::basic_string, > std::allocator > const&)' > t.cpp:(.text+0x38): undefined reference to `std::__cxx11::basic_string std::char_traits, std::allocator >::~basic_string()' > collect2: error: ld returned 1 exit status No, this is just because gcc doesn't link to the C++ runtime which is neede= d to use std::string. You will get the same link error for a correct program, it= has absolutely nothing to do with the bug in your code. > I think the coding error should be caught at the compile time by g++.=20 It is caught, by a warning. In the general case, turning all such mistakes into errors is impossible: const std::string& func(const std::string&); std::string s =3D func(s); The compiler cannot know whether func has undefined behaviour or not. It co= uld be defined like this, which would be OK: const std::string& func(const std::string& /*unused*/) { static std::string str =3D "a string with static storage duration"; } Or it could be defined like this, which would be equivalent to your example: const std::string& func(const std::string& s) { return s; } tl;dr it is not reasonable to expect to always get a compile time error for undefined behaviour, it's the programmer's responsibility to avoid undefined behaviour. COmpiler warnings can help, if you enable them and pay attention= to them.=