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.

Reply via email to