From owner-freebsd-hackers@freebsd.org Mon Jan 1 05:04:19 2018 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 388A4EB7117 for ; Mon, 1 Jan 2018 05:04:19 +0000 (UTC) (envelope-from markmi@dsl-only.net) Received: from asp.reflexion.net (outbound-mail-210-149.reflexion.net [208.70.210.149]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id D55E5761F3 for ; Mon, 1 Jan 2018 05:04:18 +0000 (UTC) (envelope-from markmi@dsl-only.net) Received: (qmail 31920 invoked from network); 1 Jan 2018 03:17:37 -0000 Received: from unknown (HELO mail-cs-02.app.dca.reflexion.local) (10.81.19.2) by 0 (rfx-qmail) with SMTP; 1 Jan 2018 03:17:37 -0000 Received: by mail-cs-02.app.dca.reflexion.local (Reflexion email security v8.40.3) with SMTP; Sun, 31 Dec 2017 22:17:37 -0500 (EST) Received: (qmail 6044 invoked from network); 1 Jan 2018 03:17:37 -0000 Received: from unknown (HELO iron2.pdx.net) (69.64.224.71) by 0 (rfx-qmail) with (AES256-SHA encrypted) SMTP; 1 Jan 2018 03:17:37 -0000 Received: from [192.168.1.25] (c-76-115-7-162.hsd1.or.comcast.net [76.115.7.162]) by iron2.pdx.net (Postfix) with ESMTPSA id 90DAFEC8171; Sun, 31 Dec 2017 19:17:36 -0800 (PST) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 10.3 \(3273\)) Subject: Re: Is it considered to be ok to not check the return code of close(2) in base? From: Mark Millard In-Reply-To: <201801010305.w0135luG084158@pdx.rh.CN85.dnsmgr.net> Date: Sun, 31 Dec 2017 19:17:35 -0800 Cc: phk@phk.freebsd.dk, FreeBSD Hackers Content-Transfer-Encoding: quoted-printable Message-Id: <559541DD-3287-4473-B7DE-B4DDC6860DF7@dsl-only.net> References: <201801010305.w0135luG084158@pdx.rh.CN85.dnsmgr.net> To: "Rodney W. Grimes" X-Mailer: Apple Mail (2.3273) X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.25 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 01 Jan 2018 05:04:19 -0000 On 2017-Dec-31, at 7:05 PM, Rodney W. Grimes = wrote: >> Poul-Henning Kamp phk at phk.freebsd.dk wrote on >> Sat Dec 30 20:35:35 UTC 2017 : >>=20 >>> But if you just close a file, and you're 100% sure that will work, >>> you should write it as: >>>=20 >>> assert(close(fd) =3D=3D 0); >>>=20 >>> To tell the rest of us about your assumption and your confidence in = it. >>=20 >> Quoting the FreeBSD assert man page: >>=20 >> QUOTE >> The assert() macro may be removed at compile time by = defining NDEBUG as a >> macro (e.g., by using the cc(1) option -DNDEBUG). >> ENDQUOTE >>=20 >> This makes required-actions inside asserts dangerous, >> at least without guarantees that NDEBUG will be >> undefined. Trying to guarantee that NDEBUG will be >> undefined would generally be a bad idea. >>=20 >> So, >>=20 >> assert(close(fd) =3D=3D 0); >>=20 >> is a bad coding practice in my view. >=20 > Less bad than either of > (void)close(fd)); > close(fd); Hmm. If one is not to check the status code, then just do not close the file, at least for NDEBUG style compiles. Seems odd to me. "assert" indicates optional code, not required code. (This is despite its name.) In my view: Both errors need to be avoided and my point still stands: the example is not a reasonable way to code. One could invent an alternate to assert under a related name that does not make code disappear (not even for NDEBUG). "assert" is just not the right facility to express just: tell the rest of us about your assumption and your confidence in it without also indicating: optional code. =3D=3D=3D Mark Millard markmi at dsl-only.net