Search code examples
cgccpthreadssetjmpcflags

pthread_cleanup_push and O2 CFLAGS


I have some warning when compiling a piece of code using pthread_cleanup_push/pop with -O2 CFLAGS. Just by removing the O2 cflags in the Makefile make it compile without issue.

Is it forbidden to use gcc optimization with these pthread macros ? I was not able to find anything in man or documentation. By the way, is there any alternative to clean stuff at the end of a thread ? Also it is working perfectly with gcc arm. But not on x86 gcc.

Warning :

x/x.c:1292:2: warning: variable ‘__cancel_routine’ might be clobbered by ‘longjmp’ or ‘vfork’ [-Wclobbered]
  pthread_cleanup_push(x_cleanup, &fd);

My current CFLAGS option :

-W -Wall -Wformat -Wformat-security -Wextra  -Wno-unused-result,
-Wextra -Wno-long-long -Wno-variadic-macros -Wno-missing-field-initializers
-std=gnu99 -O2

Solution

  • This issue has been reported several times now in GCC tracker (see here). I believe that this warns about real issue in pthread.h (see my comment). __cancel_routine is not marked as volatile so it's value is indeed undefined after return via longjmp which may cause arbitrary consequences.

    The only solution is to remove the Werror until a fix ?

    I'd rather go with -Wno-clobbered, to keep other warnings enabled.

    Roll back on a previous version of gcc on x86 ?

    You'll have to rollback to pre-2014 times which is quite a change... I think that if the code works for you, just disable -Wclobbered (with a descriptive comment).

    But I did want to be sure that its was not a bigger issue which can cause unexpected behavior of my code or bugs.

    Glibc code looks really suspicious. I'd wait for comments from GCC devs and if there is none, report this to Glibc developers.