[llvm-dev] Calling a null pointer. How undefined it is?

Gor Nishanov via llvm-dev llvm-dev at lists.llvm.org
Mon Jun 13 21:07:12 PDT 2016

Hi all:

This question is related to a state machine generated by LLVM for a coroutine.
I stripped all coroutine related details to get to the essence of the question.

Let's say I have a state machine that looks like this:

  struct State {
    FnPtr Fn;
    State() : Fn(&SomeFunction) {}

    void Go() { (*Fn)(); }
    void Stop() { Fn = nullptr; }
    bool IsDone() { return Fn == nullptr; }

Fn field serves two purposes:

* Cheap check for done. What can be better than compare with zero!
* Guard against programmer mistake: accidentally calling Go() when in a
  Stopped state.

Is it an appropriate use of undefined behavior?

Thank you,


This iassumes -O3 with no ubsan. Sanitizer can, of course, add a null
check in Go() prior to an indirect call.

More information about the llvm-dev mailing list