[BUG] try..catch does not work if compiled with clang 8.0
Pavel Fedin
p.fedin@samsung.com
Wed Dec 18 16:23:00 GMT 2019
Well, it turned out more complicated than i thought. Here is the reduced reproducer:
--- cut catch_test.cpp ---
#include <stdexcept>
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
void from_file(const std::string &filepath, std::vector<int8_t> &data);
int main()
{
try
{
std::vector<int8_t> data;
from_file("no_such_file.bin", data);
std::cout << "Success ???" << std::endl;
}
catch (const std::exception &e)
{
std::cout << "std::exception: " << e.what() << std::endl;
}
return 0;
}
--- cut catch_test.cpp ---
--- cut catch_test_2.cpp ---
#include <stdexcept>
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
using namespace std;
template <typename T> void from_file_impl(const string &filepath, vector<T> &data)
{
// data.clear();
std::ifstream ifs(filepath.c_str());
if (ifs.rdstate() & ifstream::failbit)
{
throw std::runtime_error("Error in reading " + filepath);
}
/* T buffer;
while (ifs.read(reinterpret_cast<char *>(&buffer), sizeof(T)))
{
data.push_back(buffer);
}*/
}
void from_file(const string &filepath, vector<int8_t> &data) { from_file_impl(filepath, data); }
--- cut catch_test_2.cpp ---
--- cut console log ---
CORP+p.fedin@P-FEDIN01 /cygdrive/d/Projects/Test
$ clang++-8 catch_test.cpp catch_test_2.cpp -g -o catch_test
CORP+p.fedin@P-FEDIN01 /cygdrive/d/Projects/Test
$ ./catch_test
CORP+p.fedin@P-FEDIN01 /cygdrive/d/Projects/Test
$ g++.exe catch_test.cpp catch_test_2.cpp -g -o catch_test
CORP+p.fedin@P-FEDIN01 /cygdrive/d/Projects/Test
$ ./catch_test
CORP+p.fedin@P-FEDIN01 /cygdrive/d/Projects/Test
$ clang++-5.0 catch_test.cpp catch_test_2.cpp -g -o catch_test
CORP+p.fedin@P-FEDIN01 /cygdrive/d/Projects/Test
$ ./catch_test
std::exception: Error in reading no_such_file.bin
CORP+p.fedin@P-FEDIN01 /cygdrive/d/Projects/Test
$
--- cut console log ---
So, the problem actually reproduces with both clang 8 and gcc (7.4.0 on my system). clang 5 works fine.
You could also be interested in strace (here is end of the output only, the beginning is quite long and not very interesting):
--- cut ---
22301 238862 [main] catch_test 6078 seterrno_from_nt_status: /home/corinna/src/cygwin/cygwin-3.1.0/cygwin-3.1.0-1.x86_64/src/newlib-cygwin/winsup/cygwin/fhandler.cc:724 status 0xC0000034 -> windows error 2
75 238937 [main] catch_test 6078 geterrno_from_win_error: windows error 2 == errno 2
42 238979 [main] catch_test 6078 fhandler_base::open: 0xC0000034 = NtCreateFile (0x10, 0x80100000, \??\D:\Projects\Test\no_such_file.bin, io, NULL, 0x0, 0x7, 0x1, 0x4020, NULL, 0)
78 239057 [main] catch_test 6078 fhandler_base::open: 0 = fhandler_base::open(\??\D:\Projects\Test\no_such_file.bin, 0x108000)
41 239098 [main] catch_test 6078 fhandler_base::open_fs: 0 = fhandler_disk_file::open(\??\D:\Projects\Test\no_such_file.bin, 0x8000)
99 239197 [main] catch_test 6078 open: -1 = open(no_such_file.bin, 0x8000), errno 2
--- Process 2620 (pid: 6078), exception 20474343 at 00007ff8c73fa388
--- Process 2620 (pid: 6078) thread 12836 exited with status 0x20474343
--- Process 2620 (pid: 6078) thread 3972 exited with status 0x20474343
--- Process 2620 (pid: 6078) thread 15568 exited with status 0x20474343
--- Process 2620 (pid: 6078) thread 5428 exited with status 0x20474343
--- Process 2620 (pid: 6078) thread 2364 exited with status 0x20474343
--- Process 2620 (pid: 6078) thread 16008 exited with status 0x20474343
--- Process 2620 exited with status 0x20474343
--- cut ---
So, a Windows exception is reported, then the whole thing silently quits.
I have an impression that it has to do with the bug i previously reported (and someone here claimed he could reproduce it)
Kind regards,
Pavel Fedin
Senior Engineer
Samsung Electronics Research center Russia
> -----Original Message-----
> From: cygwin-owner@cygwin.com [mailto:cygwin-owner@cygwin.com] On Behalf Of Brian Inglis
> Sent: Wednesday, December 18, 2019 3:05 AM
> To: cygwin@cygwin.com
> Subject: Re: [BUG] try..catch does not work if compiled with clang 8.0
>
> On 2019-12-17 13:51, Csaba Ráduly wrote:
> > On 17/12/2019 17:29, Brian Inglis wrote:
> >> On 2019-12-17 02:35, Pavel Fedin wrote:
> >>> I haven't upgraded for a while and today i finally decided to do so. After
> >>> rebuilding by project with clang++ 8.0 i found out that try...catch construct
> >>> doesn't work. The program just gets silently aborted.
> >>> Switched back to old clang++ 5.0 and it works.
> >>> Also an old bug which i reported some (a while) time ago persists: uncaught
> >>> throw does not print anything.
> >>> OS is Windows 10 x86/64.
> >> It is unlikely that try/catch does not work in the general case for clang++8,
> >> and your post has insufficient information to reproduce the problem.
> >
> > a.k.a. https://www.chiark.greenend.org.uk/~sgtatham/bugs.html#respect
>
> Indeed!
>
> > WJFFM
> >> $ cat catcher.cpp
> > #include <stdexcept>
> > #include <iostream>
> >
> > int pitcher()
> > {
> > throw std::runtime_error{"Ouch"};
> > }
> >
> > int main()
> > {
> > try {
> > return pitcher();
> > }
> > catch (std::exception const&e) {
> > std::cout << "Caught a " << typeid(e).name() << " - " << e.what() << '\n';
> > return 42;
> > }
> > }
> >
> > $ g++ -v -Wall -Wpedantic -Wextra -g catcher.cpp
>
> > $ ./a.exe
> > Caught a St13runtime_error - Ouch
>
> The report was about clang++ 8 vs 5; tweaked source to build under 5 and 8:
> ...
> #include <typeinfo>
>
> int pitcher()
> {
> throw std::runtime_error("Ouch");
> }
> ...
>
> but same WJFFM results:
>
> $ clang++-5.0 -g -Og -Wpedantic -Wall -Wextra -o try-catch-stc{,.cpp}
> $ ./try-catch-stc
> Caught a St13runtime_error - Ouch
>
> $ clang++-8 -g -Og -Wpedantic -Wall -Wextra -o try-catch-stc{,.cpp}
> $ ./try-catch-stc
> Caught a St13runtime_error - Ouch
>
> --
> Take care. Thanks, Brian Inglis, Calgary, Alberta, Canada
>
> This email may be disturbing to some readers as it contains
> too much technical detail. Reader discretion is advised.
>
> --
> Problem reports: https://protect2.fireeye.com/url?k=b437d571-e9e489fe-b4365e3e-
> 0cc47a31ce52-fd9c07d8a89a97f5&u=http://cygwin.com/problems.html
> FAQ: https://protect2.fireeye.com/url?k=2e1da12d-73cefda2-2e1c2a62-
> 0cc47a31ce52-67f7089b29388f59&u=http://cygwin.com/faq/
> Documentation: https://protect2.fireeye.com/url?k=3f2aeb24-62f9b7ab-3f2b606b-
> 0cc47a31ce52-9528889b998d683e&u=http://cygwin.com/docs.html
> Unsubscribe info: https://protect2.fireeye.com/url?k=b1682add-ecbb7652-b169a192-
> 0cc47a31ce52-e02defedfa7edb6b&u=http://cygwin.com/ml/#unsubscribe-simple
--
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
More information about the Cygwin
mailing list