Reputation:
According to support.signal,
An evaluation is signal-safe unless it includes one of the following:
- ...
- throwing of an exception;
- control entering a try-block or function-try-block;
- ...
A signal handler invocation has undefined behavior if it includes an evaluation that is not signal-safe.
Does it mean calling a potentially-throwing function, such as one declared with a noexcept
specifier that evaluates to false
, results in undefined behavior? Or is it only undefined behavior if the evaluation effectively results in an exception being thrown?
Here is an example:
static void g() noexcept(false) {}
extern "C" void signal_handler(int sig)
{
try {
g(); // always UB
}
catch (...) {
}
g(); // always UB?
}
Upvotes: 1
Views: 82
Reputation: 118435
"throwing of an exception" should be taken literally, and precisely: an exception gets thrown. Nothing more, nothing less.
Entering a scope that potentially throws an exception is not the same thing. As long as the code does not actually throw an exception, signal safety is not violated.
g()
does not end up throwing an exception, therefore, by itself, it is signal-safe. Even if it was not explicitly noexcept(false)
, since it does not throw an exception it is signal safe.
signal_handler()
enters a try
block, which is not signal-safe. Entering it inside a signal handler is undefined behavior.
Upvotes: 1