Are all functions in C/C++ assumed to return?

后端 未结 7 1325
抹茶落季
抹茶落季 2020-12-31 01:49

I was reading this paper on undefined behaviour and one of the example \"optimisations\" looks highly dubious:

if (arg2 == 0)
    ereport(ERROR,         


        
相关标签:
7条回答
  • 2020-12-31 02:40

    I think the answer is found, at least for C++, in section 1.9p5

    A conforming implementation executing a well-formed program shall produce the same observable behavior as one of the possible executions of the corresponding instance of the abstract machine with the same program and the same input. However, if any such execution contains an undefined operation, this International Standard places no requirement on the implementation executing that program with that input (not even with regard to operations preceding the first undefined operation).

    In fact, the macro expands to a call to errstart which will return (ERROR >= ERROR), obviously true. That triggers a call to errfinish which calls proc_exit which runs some registered cleanup and then the Standard runtime function exit. So there is no possible execution that contains a divide-by-zero. However, the compiler logic testing this must have gotten it wrong. Or perhaps an earlier version of the code failed to properly exit.

    0 讨论(0)
提交回复
热议问题