I needed to check the error code of std::system_error
doing my project. But when I did that, I ended up with SIGSEGV
.
foo.cpp
:
#include <thread>
#include <iostream>
#include <system_error>
int main(void)
{
std::thread t1;
try
{
t1.join();
}
catch (const std::system_error& e)
{
std::cout << "System error\n";
if (e.code() == std::errc::invalid_argument) // here in the operator==(), running into SIGSEGV
{
std::cout << e.what() << "\n";
}
else
{
throw;
}
}
return 0;
}
Makefile
:
.PHONY: all clean
all: foo
SRCS := $(shell find . -name '*.cpp')
OBJS := $(SRCS:.cpp=.o)
foo: $(OBJS)
gcc -o $@ $^ -lstdc++ -lpthread
%.o: %.cpp
gcc -std=c++17 -Wall -c -g -O0 -pthread -o $@ $<
clean:
rm -rf foo *.o
My gcc version:
$ gcc --version
gcc (GCC) 7.2.1 20170829 (Red Hat 7.2.1-1)
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.
When I was posting this question, StackOverflow suggested similar questions. One of them is How to portably compare std::system_error exceptions to std::errc values?. What is described in it seemed to be closely related to the problem I am going through. So I tested against gcc 8.3 which was in the list of problem fixed gcc versions.
Fixed on all active branches, so will be fixed in the 6.5, 7.4, 8.3 and 9.1 releases.
But it still appeared to have not been fixed, or it is just another problem:
$ gcc --version
gcc (GCC) 8.3.1 20190311 (Red Hat 8.3.1-3)
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.
$ make
gcc -std=c++17 -Wall -c -g -O0 -pthread -o foo.o foo.cpp
gcc -o foo foo.o -lstdc++ -lpthread
$ ./foo
System error
Segmentation fault (core dumped)
Any ideas to work around this problem and detect which error actually it is when the system_error
occurs?
Aucun commentaire:
Enregistrer un commentaire