https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117267

            Bug ID: 117267
           Summary: 'setjmp' can never be copied because it receives a
                    non-local goto
           Product: gcc
           Version: 14.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: stsp at users dot sourceforge.net
  Target Milestone: ---

The test code below works with
clang but is rejected by gcc:

---
#include <stdio.h>
#if 0
#include <setjmp.h>
#else
typedef struct __jmp_buf {
  void *buf[5];
  volatile int rc;
} jmp_buf[1];

__attribute__((noreturn))
static inline void longjmp(jmp_buf env, int val)
{
  env[0].rc = val;
  __builtin_longjmp(env[0].buf, 1);
}

__attribute__((returns_twice,always_inline))
static inline int setjmp(jmp_buf env)
{
  int rc = __builtin_setjmp(env[0].buf);
  return (rc ? env[0].rc : 0);
}
#endif

static jmp_buf jb;

static void foo(void)
{
    longjmp(jb, 1);
}

int main(void)
{
    if (setjmp(jb))
        printf("Jumped\n");
    else
        foo();
    return 0;
}
---

If I remove attribute always_inline,
which is obviously a wrong thing to do,
then the compilation succeeds, but gcc
generates the nonsentical code. clang
in that case still generates correct code,
but of course you grab the invalid stack
frame, so its not working as expected.
But gcc for some reason generates garbage.

Reply via email to