Skip to content

Commit 7e1bec1

Browse files
peffgitster
authored andcommitted
run-command: use errno to check for sigfillset() error
Since enabling -Wunreachable-code, builds with clang on macOS now fail, complaining that the die_errno() call in: if (sigfillset(&all)) die_errno("sigfillset"); is unreachable. On that platform the manpage documents that sigfillset() always returns success, and presumably the implementation is a macro or inline function that does so in a way that is transparent to the compiler. But we should continue to check on other platforms, since POSIX says it may return an error. We could solve this with a compile-time knob to split the two cases (assuming success on macOS and checking for the error elsewhere). But we can also work around it more directly by relying on errno to check the outcome (since POSIX dictates that errno will be set on error). And that works around the compiler's cleverness, since it doesn't know the semantics of errno (though I suppose if sigfillset() is simple enough, it could perhaps realize that no writes to errno are possible; however this does seem to work in practice). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f93ff17 commit 7e1bec1

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

run-command.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,15 @@ static void atfork_prepare(struct atfork_state *as)
515515
{
516516
sigset_t all;
517517

518-
if (sigfillset(&all))
518+
/*
519+
* Do not use the return value of sigfillset(). It is transparently 0
520+
* on some platforms, meaning a clever compiler may complain that
521+
* the conditional body is dead code. Instead, check for error via
522+
* errno, which outsmarts the compiler.
523+
*/
524+
errno = 0;
525+
sigfillset(&all);
526+
if (errno)
519527
die_errno("sigfillset");
520528
#ifdef NO_PTHREADS
521529
if (sigprocmask(SIG_SETMASK, &all, &as->old))

0 commit comments

Comments
 (0)