Package: g++-5
Version: 5.1.1-9
Severity: normal

--- Please enter the report below this line. ---

The attached simple C++ source compiles fine but causes a segmentation fault using current version of g++-5 and optimization levels -O2 or -O3. The same code compiles and runs fine with g++ 4.9.1 with either optimization level so it appears there is a problem with the optimizer in version 5.

To reproduce:

$ g++-5 -O2 -g code.cpp
$ gdb -ex run a.out
(...)
Reading symbols from a.out...done.
Starting program: /home/user/a.out

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff75c93d7 in _Unwind_Resume ()
   from /lib/x86_64-linux-gnu/libgcc_s.so.1

Compiling with -O1 or lower produces the expected output when running:
$ g++-5 -O1 test.cpp
$ ./a.out
terminate called after throwing an instance of 'MyException'
  what():  MyException: No!
Aborted


--- System information. ---
Architecture: amd64
Kernel:       Linux 3.16.0-4-amd64

Debian Release: stretch/sid
  500 testing         mirror.aarnet.edu.au
  500 stable          security.debian.org
  500 stable          mirror.aarnet.edu.au


#include <exception>
#include <string>

class MyException : public std::exception
{
protected:
    typedef std::exception Parent;

public:
    MyException(const char* cStr): Parent(), m_reason(cStr) {
        updateMessage();   
    }

    virtual ~MyException() throw() {}

    virtual const std::string& exceptionName() const {
        return exceptionNameValue;
    }

    virtual const char* what() const throw() {
        return m_exceptionMessage.c_str();
    }

    inline void updateMessage() {
        m_exceptionMessage = exceptionName() + ": " + m_reason;
    }

private:
    std::string m_reason;
    std::string m_exceptionMessage;
    static const std::string exceptionNameValue;
};

const std::string MyException::exceptionNameValue("MyException");

int main() {
    throw MyException("No!");
    return 0;
}

Reply via email to