Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 8 Jan 2018 10:13:19 -0800
From:      Conrad Meyer <cem@freebsd.org>
To:        Andrew Duane <aduane@juniper.net>
Cc:        Freebsd hackers list <freebsd-hackers@freebsd.org>
Subject:   Re: Is it considered to be ok to not check the return code of close(2) in base?
Message-ID:  <CAG6CVpXq5rx6HW=UuHD29D7EZPr0z7LfZ4Rox1j6u=qMyGvpnQ@mail.gmail.com>
In-Reply-To: <SN1PR0501MB212559C6D852EBEEA3A89C7CCE130@SN1PR0501MB2125.namprd05.prod.outlook.com>
References:  <201801081655.w08GtO3D022568@pdx.rh.CN85.dnsmgr.net> <df6f98a5-76db-d6d8-6321-d35b59eeec22@vangyzen.net> <SN1PR0501MB212559C6D852EBEEA3A89C7CCE130@SN1PR0501MB2125.namprd05.prod.outlook.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Mon, Jan 8, 2018 at 10:05 AM, Andrew Duane <aduane@juniper.net> wrote:
> Of course, my OCD will kick in and say this would need to be something like:
>
> #ifdef DEBUG_CLOSE
> #define close(f)        do {if (close(f) < 0) assert(errno != EBADF); } while (0)
> #endif
>
> Have to watch those macro replacements like "if (need_to_close) close(f);". And the close succeeding :-)

Of course, this has turned into nerd sniping.  But I'll take my turn.

Better to use the GCC "statement expression" extension so that the
return value of "close()" can still be used by callers naive to the
macro.

#define close(f)    ({ int __ret;  __ret = (close)(f);  if (__ret < 0)
assert(errno != EBADF);  __ret; })

Best,
Conrad



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CAG6CVpXq5rx6HW=UuHD29D7EZPr0z7LfZ4Rox1j6u=qMyGvpnQ>