Disable spurious -Wstringop-overflow for setjmp/longjmp (bug 26647)

Joseph Myers joseph@codesourcery.com
Thu Oct 29 17:26:28 GMT 2020


On Thu, 29 Oct 2020, Andreas Schwab wrote:

> On Okt 29 2020, Joseph Myers wrote:
> 
> > Like this (which also works to fix the testsuite build problems, given the 
> > three other patches posted to fix the build of glibc itself)?
> >
> > diff --git a/sysdeps/nptl/pthread.h b/sysdeps/nptl/pthread.h
> > index d4194da..6632eae 100644
> > --- a/sysdeps/nptl/pthread.h
> > +++ b/sysdeps/nptl/pthread.h
> > @@ -658,8 +658,9 @@ __pthread_cleanup_routine (struct __pthread_cleanup_frame *__frame)
> >      __pthread_unwind_buf_t __cancel_buf;				      \
> >      void (*__cancel_routine) (void *) = (routine);			      \
> >      void *__cancel_arg = (arg);						      \
> > -    int __not_first_call = __sigsetjmp ((struct __jmp_buf_tag *) (void *)     \
> > -					__cancel_buf.__cancel_jmp_buf, 0);    \
> > +    int __not_first_call						      \
> > +      = __sigsetjmp_cancel ((struct __jmp_buf_tag *) (void *)		      \
> > +			    __cancel_buf.__cancel_jmp_buf, 0);		      \
> 
> Why not just using the type of __cancel_jmp_buf directly?

Here is a patch that (gives that type a name and) does that.

I haven't seen any comments on the three patches to fix the build of 
glibc.

diff --git a/sysdeps/nptl/pthread.h b/sysdeps/nptl/pthread.h
index d4194da..bcea530 100644
--- a/sysdeps/nptl/pthread.h
+++ b/sysdeps/nptl/pthread.h
@@ -512,13 +512,15 @@ extern void pthread_testcancel (void);
 
 /* Cancellation handling with integration into exception handling.  */
 
+struct __cancel_jmp_buf_tag
+{
+  __jmp_buf __cancel_jmp_buf;
+  int __mask_was_saved;
+};
+
 typedef struct
 {
-  struct
-  {
-    __jmp_buf __cancel_jmp_buf;
-    int __mask_was_saved;
-  } __cancel_jmp_buf[1];
+  struct __cancel_jmp_buf_tag __cancel_jmp_buf[1];
   void *__pad[4];
 } __pthread_unwind_buf_t __attribute__ ((__aligned__));
 
@@ -658,8 +660,8 @@ __pthread_cleanup_routine (struct __pthread_cleanup_frame *__frame)
     __pthread_unwind_buf_t __cancel_buf;				      \
     void (*__cancel_routine) (void *) = (routine);			      \
     void *__cancel_arg = (arg);						      \
-    int __not_first_call = __sigsetjmp ((struct __jmp_buf_tag *) (void *)     \
-					__cancel_buf.__cancel_jmp_buf, 0);    \
+    int __not_first_call = __sigsetjmp_cancel (__cancel_buf.__cancel_jmp_buf, \
+					       0);			      \
     if (__glibc_unlikely (__not_first_call))				      \
       {									      \
 	__cancel_routine (__cancel_arg);				      \
@@ -693,8 +695,8 @@ extern void __pthread_unregister_cancel (__pthread_unwind_buf_t *__buf)
     __pthread_unwind_buf_t __cancel_buf;				      \
     void (*__cancel_routine) (void *) = (routine);			      \
     void *__cancel_arg = (arg);						      \
-    int __not_first_call = __sigsetjmp ((struct __jmp_buf_tag *) (void *)     \
-					__cancel_buf.__cancel_jmp_buf, 0);    \
+    int __not_first_call = __sigsetjmp_cancel (__cancel_buf.__cancel_jmp_buf, \
+					       0);			      \
     if (__glibc_unlikely (__not_first_call))				      \
       {									      \
 	__cancel_routine (__cancel_arg);				      \
@@ -730,9 +732,24 @@ extern void __pthread_unwind_next (__pthread_unwind_buf_t *__buf)
      ;
 #endif
 
-/* Function used in the macros.  */
+/* Function used in the macros.  Calling __sigsetjmp, with its first
+   argument declared as an array, results in a -Wstringop-overflow
+   warning because struct pthread_unwind_buf is smaller than jmp_buf.
+   The calls from the macros have __SAVEMASK set to 0, so nothing
+   beyond the common prefix is used and this warning is a false
+   positive.  Use an alias with its first argument declared to use the
+   type in the macros if possible to avoid this warning.  */
+#ifdef __REDIRECT_NTHNL
+extern int __REDIRECT_NTHNL (__sigsetjmp_cancel,
+			     (struct __cancel_jmp_buf_tag __env[1],
+			      int __savemask),
+			     __sigsetjmp);
+#else
+# define __sigsetjmp_cancel(env, savemask) \
+  __sigsetjmp ((struct __jmp_buf_tag *) (void *) (env), (savemask))
 extern int __sigsetjmp (struct __jmp_buf_tag __env[1],
 			int __savemask) __THROWNL;
+#endif
 
 
 /* Mutex handling.  */

-- 
Joseph S. Myers
joseph@codesourcery.com


More information about the Libc-alpha mailing list