Web lists-archives.com

iostream exceptions uncaught with CYGWIN GCC/7.3.0




Hi,

Please consider the following code:

$ cat bug.cpp 
#include <iostream>

using namespace std;

void fun()
{
    string dummy;
    cin >> dummy;
}


int main()
{
    cout << "FAIL = 0x" << hex << ios::failbit << endl;
    cout << "EOF  = 0x" << hex << ios::eofbit  << endl;
    cout << "BAD  = 0x" << hex << ios::badbit  << endl;

    cin.exceptions(ios::eofbit | ios::failbit);
    try {
        fun();
    }
    catch (ios_base::failure&) {
        cerr << "Failure caught!" << endl;
    }
    catch (...) {
        cerr << "Failure uncaught! 0x" << hex << cin.rdstate() << endl;
    }
    return 0;
}

When this program is compiled and run on Linux, the exception gets caught:

$ uname -a
Linux iebdev11 3.10.0-862.14.4.el7.x86_64 #1 SMP Wed Sep 26 15:12:11 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

$ g++ --version
g++ (GCC) 7.3.0
Copyright (C) 2017 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.

$ g++ -Wall bug.cpp -o bug

$ ./bug < /dev/null
FAIL = 0x4
EOF  = 0x2
BAD  = 0x1
Failure caught!

However, same commands on Cygwin, and the exception goes unhandled:

$ uname -a
CYGWIN_NT-10.0 NCBIPC9135 2.11.1(0.329/5/3) 2018-09-05 10:24 x86_64 Cygwin

$ gcc --version
gcc (GCC) 7.3.0
Copyright (C) 2017 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.

$ g++ -Wall -o bug bug.cpp

$ ./bug < /dev/null
FAIL = 0x4
EOF  = 0x2
BAD  = 0x1
Failure uncaught! 0x6

We've seen this behavior before on Linux too, when the C++ ABI was changed (w/GCC 5.x).  I guess CYGWIN packages a version of C++ STDLIB whose ABI is incompatible with default compiler settings.

Here's some explanation of what might be the culprit.

https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html says that:

> Using the default configuration options for GCC the default value of the macro is 1 which causes the new ABI to be active, so to use the old ABI you must explicitly define the macro to 0 before including any library headers.
>  (Be aware that some GNU/Linux distributions configure GCC 5 differently so that the default value of the macro is 0 and users must define it to 1 to enable the new ABI.)

> One exception type does change when using the new ABI, namely std::ios_base::failure. This is necessary because the 2011 standard changed its base class from std::exception to std::system_error, which causes its layout to change. Exceptions due to iostream errors are thrown by a function inside libstdc++.so, so whether the thrown exception uses the old std::ios_base::failure type or the new one depends on the ABI that was active when libstdc++.so was built, not the ABI active in the user code that is using iostreams. This means that for a given build of GCC the type thrown is fixed. In current releases the library throws a special type that can be caught by handlers for either the old or new type, but for GCC 7.1, 7.2 and 7.3 the library throws the new std::ios_base::failure type, and for GCC 5.x and 6.x the library throws the old type. Catch handlers of type std::ios_base::failure will only catch the exceptions if using a newer release, or if the handler is compiled with the same ABI as the type thrown by the library. Handlers for std::exception will always catch iostreams exceptions, because the old and new type both inherit from std::exception.

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple