Check if jump buffer really works (not local jumps)

We have implemented the "longjmp-Restore stack environment" in our code base. The procedure longjmpis called by a specific function error_exitthat can be called from anywhere.

Thus, it is possible that the longjmpsubroutine setjmpmay not be called during a call , and the buffer may have an invalid value, resulting in a failure.

Can I initialize the buffer before NULLor is there any check to check for an invalid or invalid value. One way is that I can set the flag variable whenever called setjmp, and I can check it. But this is just a hack.

void error_exit()
{
    extern jmp_buf buf;
    longjmp(buf, 1);
    return 1;
}

Can I do something like this?

void error_exit()
{
    extern jmp_buf buf;

    if(buf)
       longjmp(buf, 1);

    return 1;
}

C/++, , setjmp longjmp ++ , , longjmp , ?

+4
3

jmp_buf . linux - :

typedef int __jmp_buf[6];

struct __jmp_buf_tag {
  __jmp_buf __jmpbuf;       /* Calling environment.  */
  int __mask_was_saved;     /* Saved the signal mask?  */
  __sigset_t __saved_mask;  /* Saved signal mask.  */
};

typedef struct __jmp_buf_tag jmp_buf[1];

.

, NULL setjmp.

  jmp_buf physical_buf;
  jmp_buf *buf = NULL;
  ...
  buf = &physical_buf;
  if (setjmp(*buf)) {
    ...
  }

, . , jmp .

+1

, , , , .

, longjmp , undefined . , setjmp, . , setjmp . jmp_buf, , .

, , , , , , , setjmp.

+2

.

POSIX (http://pubs.opengroup.org/onlinepubs/9699919799/functions/longjmp.html http://pubs.opengroup.org/onlinepubs/9699919799/functions/setjmp.html http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/setjmp.h.html) jmp_buf , , , :

<setjmp.h> jmp_buf ... sigjmp_buf.

, bzero() - , , jmp_buf , POSIX-, ( ), set_jmp - , , memcmp jmp_buf jmp_buf, . , (.. set_jmp) jmp_buf s.

, , ( struct).

I believe that I should insert a mandatory warning on manpage re set_jmpand long_jmp, since it makes it difficult to read applications and has unpredictable consequences for signal processing (the latter is cured with sigsetjmp()). Also, in the context of C ++, it does not explicitly unwind exception handlers.

+1
source

Source: https://habr.com/ru/post/1524241/


All Articles