I was reading this paper on undefined behaviour and one of the example \"optimisations\" looks highly dubious:
if (arg2 == 0) ereport(ERROR,
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.