public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* Bad interaction between signal constants and -Wsign-conversion
@ 2023-03-28  1:23 Zack Weinberg
  2023-03-28  1:58 ` Paul Eggert
  2023-03-29  0:08 ` Alejandro Colomar
  0 siblings, 2 replies; 11+ messages in thread
From: Zack Weinberg @ 2023-03-28  1:23 UTC (permalink / raw)
  To: GNU libc development

This test program does something perfectly normal.  Well, it's cut down
from a program that does something perfectly normal.  SA_RESETHAND is
one of the bit flags intended to be used with the sa_flags field of
struct sigaction.

#include <signal.h>

struct sigaction sa = { 
  .sa_flags = SA_RESETHAND,
};

However ... on Linux, SA_RESETHAND happens to have the value 0x80000000,
and therefore, the type of that constant is 'unsigned int'.  And the
type of sa_flags is 'int'.  So if you compile this test program with
-Wconversion you get a warning:

$ gcc -fsyntax-only -Wconversion test.c
test.c:4:15: warning: signed conversion from ‘unsigned int’ to ‘int’
   changes value from ‘2147483648’ to ‘-2147483648’ [-Wsign-conversion]
    4 |  .sa_flags = SA_RESETHAND,
      |              ^~~~~~~~~~~~

This strikes me as a (minor) bug in our headers.  Given the ABI constraints
(this is a flags word passed to the kernel), I think the most straightforward
fix is probably to change the #define in bits/sigaction.h to

# define SA_RESETHAND ((int)0x80000000) /* Reset to SIG_DFL on entry to handler.  */

Anyone got a better idea?

zw

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2023-03-31 15:31 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-28  1:23 Bad interaction between signal constants and -Wsign-conversion Zack Weinberg
2023-03-28  1:58 ` Paul Eggert
2023-03-28 14:03   ` Cyril Hrubis
2023-03-28 14:53     ` Florian Weimer
2023-03-28 15:04       ` Andreas Schwab
2023-03-28 18:50         ` Florian Weimer
2023-03-29  0:08 ` Alejandro Colomar
2023-03-30 13:45   ` Zack Weinberg
2023-03-30 13:59   ` Vincent Lefevre
2023-03-30 14:09     ` Alejandro Colomar
2023-03-31 15:31       ` Vincent Lefevre

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).