Is it safe to use va_list in exception-prone code?

后端 未结 3 2352
生来不讨喜
生来不讨喜 2021-02-20 01:32

Typical example:

void foo(const char *fmt, ...)
{
    va_list args;
    va_start(args, fmt);

    // might throw, might not.  who knows.
    bar(fmt, args);

            


        
3条回答
  •  独厮守ぢ
    2021-02-20 02:22

    The C++ standard defers to the C standard for the specification of va_start et al. The C standard has this to say:

    7.15.1p1 ...Each invocation of the va_start and va_copy macros shall be matched by a corresponding invocation of the va_end macro in the same function.

    Thus, if you exit the function by any means after calling va_start but before va_end, your program exhibits undefined behavior.

    Yes, wrapping bar in a try/catch would help.

提交回复
热议问题