Is there a gcc option to assume all extern “C” functions cannot propagate exceptions?

前端 未结 4 1427
别那么骄傲
别那么骄傲 2021-02-01 16:35

Is there any way, short of putting an attribute on each function prototype, to let gcc know that C functions can never propagate exceptions, i.e. that all functions declared ins

4条回答
  •  日久生厌
    2021-02-01 17:34

    Side note:
    Are you sure telling the compiler "all these funcs never throw" is exactly what you want ?

    It's not necessarily so that extern "C" ... functions cannot propagate/trigger exceptions. Take a case in point:

    class Foo {
    public:
        class Away {};
        static void throwaway(void) { throw Away(); }
    }
    
    extern "C" {
        void wrap_a_call(void (*wrapped)(void)) { wrapped(); }
    }
    
    int main(int argc, char **argv)
    {
        wrap_a_call(Foo::throwaway);
        return 0;
    }
    

    Compiling and running this creates a C-linkage function wrap_a_call() which, when called like above, will happily cause an exception:

    $ ./test
    terminate called after throwing an instance of 'Foo::Away'
    Abort(coredump)
    

    I.e. there can be "exception leakage" with extern "C" (through invoking function pointers) and just because you're using/invoking extern "C" functions in a particular place in C++ doesn't guarantee no exceptions can be thrown when invoking those.

提交回复
热议问题