https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118263
Bug ID: 118263
Summary: Compiler should issue a warning when passing function
pointers not marked noexcept to functions where
throwing an exception in a callback function is
undefined behavior
Product: gcc
Version: 14.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: richard.yao at alumni dot stonybrook.edu
Target Milestone: ---
The unity blog describes an interesting bug that occurred on Windows:
https://unity.com/blog/engine-platform/debugging-memory-debugging-memory-corruption-who-wrote-2-into-my-stack-who-the-hell
Thread A blocked in select(), which is a wrapper around
WaitForSingleObjectEx(). Thread B called QueueUserAPC(), which interrupted
Thread A to run a callback function. The callback function then threw an
exception, causing the stack to unwind, while the kernel had yet to respond to
the select() call. When it finally did respond to the select call, the stack
frame was gone due to C++ stack unwinding, and WAIT_TIMEOUT (0x00000102L) was
written to the stack, causing stack corruption.
WaitForSingleObjectEx() is a C ABI function and thus invoking a C++ exception
in it triggers undefined behavior. The compiler should issue a warning when
passing a function pointer not marked noexcept to a C ABI function, but does
not:
https://godbolt.org/z/14ocshsE5
Similarly, the compiler should warn about passing a function pointer to a
function not marked noexcept to a function marked noexcept, but does not:
https://godbolt.org/z/rjPfYjnzf