-
Notifications
You must be signed in to change notification settings - Fork 12.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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 #121427
Comments
https://godbolt.org/z/sP7YM1xYT -- you are not marked a functiont pointer with <source>:14:5: error: no matching function for call to 'noexceptFunction'
14 | noexceptFunction(nonNoexceptFunction); // Warning should happen here, but does not.
| ^~~~~~~~~~~~~~~~
<source>:4:6: note: candidate function not viable: no known conversion from 'void ()' to 'void (*)() noexcept' for 1st argument
4 | void noexceptFunction(void (*func)() noexcept) noexcept {
| ^ ~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
Compiler returned: 1 |
expected behavior is: c++11 (N3337), c++14 (N4140) (except.spec p9):
c++17 (N4659), c++20 (N4868), c++23 (N4950) ("called" replace with "invoked") (except.spec p5) :
|
The compiler should generate an error for the rule:
|
This is CWG1555 which is unfortunately closed as NAD now. Most compilers don't make C and C++ language linkages differentiate function types, which is currently non-conforming. I don't think compilers should change for this, as doing so will break too much code. Perhaps someone should write a paper targeting WG21/EWG to properly resolve CWG1555. |
Actually, there's no such rule in C++ (or in C which can't specify anything for C++ exceptions). In a conforming C++ implementation (note that the |
This is the same as Linus Torvald's "bug report" about the supposedly broken memcpy function in GCC. The linux kernel code had undefined behavior, but he thought that the compiler was the one with the bug. But I think that we shouldn't rely on "dirty" code bases, or at least make it possible for programmers who don't make bad code to have the compiler behave normally in accordance with the standard. Code that relies on non-standard behavior cannot claim to work normally. upd. The lack of a compilation error in this case can lead to very hard to catch bugs when code relies on such overloads. I think compiler developers could consider this as well. |
Is it reasonable to expect C headers to mark their functions noexcept(false) when that is not valid C? |
Ah, but when the code attempts to rely on standard behavior, it often doesn't compile (already reported in #15935). When all of
|
See also Despite closing CWG1555 as NAD, EWG didn't seem to request implementations to change... |
@safocl please use latest draft when quoting and use |
how latest draft to use? i used before from the latest released standard c++23 |
You can use https://eel.is/c++draft. But I'd say link to the latest draft is somehow unstable, and a released NXXXX draft might be preferred for stability. |
It can be but it also includes issue resolutions and we are often dealing w/ core issues. If you are going to point to a specific standard, sometimes that makes sense then we should specify that but I would check if the wording has changed in the latest draft as well. For most issues the latest draft works just fine. |
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.
QueueUserAPC() 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/7hKnrW1xe
Similarly, the compiler should warn about passing a function pointer not marked noexcept to a function marked noexcept, but does not:
https://godbolt.org/z/bMab4a4YG
There is also a bug filed with GCC, which similarly does not issue a warning:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118263
The text was updated successfully, but these errors were encountered: