title: Is signal.h a reliable way to catch null pointers?
body: <p>i am currently writing a small VM in C/C++. Obviously i can't let the whole VM crash if the user dereferences a null pointer so i have to check every access which is becoming cumbersome as the VM grows and more systems are implemented.</p>
  
  <p>So i had an idea: write a signal handler for sigsegv and let the OS do its thing but instead of closing the program call the VM exception handler. </p>
  
  <p>It seems to work (with my very simple test cases), but i didn't find anything guaranteeing a Sigsegv being thrown on null-derefs nor the handler being called for OS generated signals. </p>
  
  <p>So my question is:
  Can i count on signal.h on modern destkop OSes (i don't really care if it's not standard on doesn't work on something other than linux/win: it's a pet project). Are there any non trivial stuff i should be aware of (obscure limitations of signal(...) or longjmp(...) ?)</p>
  
  <p>Thank you !</p>
  
  <p>Here is the pseudo implementation: </p>
  
  <pre><code>/* ... */
  
  jmp_buf env;
  
  /* ... */
  
  void handler(int) {
      longjmp(env, VM_NULLPTR);
  }
  
  /* ... */
  
  if(setjmp(env)) {
      return vm_throw("NullPtrException");
  } 
  switch(opcode) {
  
      /* instructions */
  
      case INVOKE:
          *stack_top = vm_call(stack_top-&gt;obj); // don't check anything in the case where stack_top or stack_top-&gt;obj is null handler() will be called an a "NullPtrException" will be thrown
      break;
  
      /* more instructions */
  
  }
  
  /* ... */
  </code></pre>
  
  <p>Note : i only need to check nulls, garbage (dangling) pointers are handled by the GC and should not happen.</p>