"GOT", but the "O" is a cute, smiling pufferfish. Index | Thread | Search

From:
Theo Buehler <theo@theobuehler.org>
Subject:
Re: mark got_privsep_exec_child as __dead
To:
Thomas Adam <thomas@xteddy.org>, Omar Polo <op@omarpolo.com>, gameoftrees@openbsd.org
Date:
Sat, 18 Dec 2021 00:44:56 +0100

Download raw body.

Thread
> So __dead and __attribute__ ((__noreturn__)) are different from
> a void return type, in the sense that they inform the compiler
> that the program will stop its execution at the end of the function?
> 
> What is the advantage over just calling exit()?
> 

It's a hint for the compiler. I think it allows some optimizations like
eliminating dead code.  It will also prevent some warnings. As a silly
example, compiling the program below will generate a warning:

test.c:17:1: warning: non-void function does not return a value in all control paths [-Wreturn-type]

If you mark g() __dead, it will compile without warnings as it should
because g won't return. If you call exit directly in h() instead of
going via g(), the compiler will be happy since exit() is marked __dead
in stdlib.h.

#include <stdio.h>
#include <stdlib.h>

void
g(void)
{
	exit(0);
}

int
h(int exit)
{
	if (exit)
		g();
	else
		return 42;
}

int
main(void)
{
	h(1);

	return 0;
}