Date: Wed, 10 Jan 2018 11:26:30 +0100 From: Stefan Esser <se@freebsd.org> To: "Rodney W. Grimes" <freebsd-rwg@pdx.rh.CN85.dnsmgr.net>, Eric van Gyzen <eric@vangyzen.net> Cc: Brooks Davis <brooks@freebsd.org>, Ian Lepore <ian@freebsd.org>, Alan Somers <asomers@freebsd.org>, Freebsd hackers list <freebsd-hackers@freebsd.org>, Yuri <yuri@rawbw.com>, Eugene Grosbein <eugen@grosbein.net> Subject: Re: Is it considered to be ok to not check the return code of close(2) in base? Message-ID: <b7d5cc97-2042-42ff-914c-35a28560932b@freebsd.org> In-Reply-To: <201801081800.w08I0D0q022877@pdx.rh.CN85.dnsmgr.net> References: <201801081800.w08I0D0q022877@pdx.rh.CN85.dnsmgr.net>
next in thread | previous in thread | raw e-mail | index | archive | help
Am 08.01.18 um 19:00 schrieb Rodney W. Grimes: >> On 01/08/2018 10:55, Rodney W. Grimes wrote: >>>> 08.01.2018 23:13, Eric van Gyzen wrote: >>>> >>>>> Right, which is the reason such bugs are hard to diagnose. Optionally >>>>> killing the process on close->EBADF would help find buggy code when >>>>> another thread did NOT re-open the file descriptor between the two close >>>>> calls. >>>> >>>> Wouldn't "close(f); assert(errno != EBADF);" be better? >> >> Putting the code in one place is far better than putting it in N >> places...after /finding/ those N places. Indeed, the purpose of this >> code is to help people find those places, even in their own code, >> outside of base. > > I agree with that. > >>> Or even >>> #ifdef DEBUG_CLOSE >>> #define close(f) close(f); assert(errno != EBADF); >>> #endif >> >> errno could have been EBADF before the close(). A successful close() >> does not modify errno. So, this would have be larger, making it even >> more unpalatable. > > Ok, so lets get a bit more clever, > #ifdef DEBUG_CLOSE > #define close(f) assert(close(f) && errno != EBADF) > #endif This will lead to close() being removed from the program, if NDEBUG is defined ... So, at least test for NDEBUG in addition to DEBUG_CLOSE, to enable this macro.
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?b7d5cc97-2042-42ff-914c-35a28560932b>