public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "redi at gcc dot gnu.org" <gcc-bugzilla@gcc.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 [thread overview] Message-ID: <bug-101850-4-krIQof6tMR@http.gcc.gnu.org/bugzilla/> (raw) In-Reply-To: <bug-101850-4@http.gcc.gnu.org/bugzilla/> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101850 --- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> --- (In reply to Prasanta Behera from comment #0) > Version info: > ============= > ~$ 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 PURPOSE. > > ~$ 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 PURPOSE. > > Sample Program: > =============== > ~$ cat t.cpp > #include <string> > > int main() { > std::string s = s; > return 0; > } > > Problem Description: > ==================== > 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 expect 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) > > gcc however shows a link time error which is slightly better. > > ~$ gcc t.cpp > /tmp/cc30YkgH.o: In function `main': > t.cpp:(.text+0x27): undefined reference to `std::__cxx11::basic_string<char, > std::char_traits<char>, std::allocator<char> > >::basic_string(std::__cxx11::basic_string<char, std::char_traits<char>, > std::allocator<char> > const&)' > t.cpp:(.text+0x38): undefined reference to `std::__cxx11::basic_string<char, > std::char_traits<char>, std::allocator<char> >::~basic_string()' > collect2: error: ld returned 1 exit status No, this is just because gcc doesn't link to the C++ runtime which is needed 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++. 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 = func(s); The compiler cannot know whether func has undefined behaviour or not. It could be defined like this, which would be OK: const std::string& func(const std::string& /*unused*/) { static std::string str = "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.
next prev parent reply other threads:[~2021-08-10 19:42 UTC|newest] Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top 2021-08-10 19:04 [Bug c++/101850] New: Initialising a std::string variable to itself does not fail at compile time, but throws std::bad_alloc at run time prasantabehera at hotmail dot com 2021-08-10 19:09 ` [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) pinskia at gcc dot gnu.org 2021-08-10 19:11 ` pinskia at gcc dot gnu.org 2021-08-10 19:42 ` redi at gcc dot gnu.org [this message] 2021-08-10 19:48 ` prasantabehera at hotmail dot com 2021-08-10 19:51 ` redi at gcc dot gnu.org 2021-08-10 20:00 ` redi at gcc dot gnu.org
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=bug-101850-4-krIQof6tMR@http.gcc.gnu.org/bugzilla/ \ --to=gcc-bugzilla@gcc.gnu.org \ --cc=gcc-bugs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
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).