From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/13703 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: closedir() Date: Thu, 31 Jan 2019 21:30:07 -0500 Message-ID: <20190201023007.GL23599@brightrain.aerifal.cx> References: <20190201002716.GK23599@brightrain.aerifal.cx> Reply-To: musl@lists.openwall.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="153797"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Mutt/1.5.21 (2010-09-15) To: musl@lists.openwall.com Original-X-From: musl-return-13719-gllmg-musl=m.gmane.org@lists.openwall.com Fri Feb 01 03:30:23 2019 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.89) (envelope-from ) id 1gpOaz-000dut-SF for gllmg-musl@m.gmane.org; Fri, 01 Feb 2019 03:30:21 +0100 Original-Received: (qmail 7460 invoked by uid 550); 1 Feb 2019 02:30:19 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 7442 invoked from network); 1 Feb 2019 02:30:19 -0000 Content-Disposition: inline In-Reply-To: Original-Sender: Rich Felker Xref: news.gmane.org gmane.linux.lib.musl.general:13703 Archived-At: On Fri, Feb 01, 2019 at 01:03:17AM +0000, Jorge Almeida wrote: > On Fri, Feb 1, 2019 at 12:27 AM Rich Felker wrote: > > > > On Thu, Jan 31, 2019 at 09:58:06PM +0000, De Paula, Judah wrote: > > > > http://man7.org/linux/man-pages/man3/closedir.3.html > > > > Hi, > > > > Passing an invalid pointer to closedir is undefined behavior, and in > > musl the preferred effect for undefined behavior when it's not costly > > to attempt to detect is a quick crash so that the point of error in > > the program can be identified and fixed. > > > > Is there a standard that says it is undefined behavior? According to > the man page quoted by the OP, and also according to > http://pubs.opengroup.org/onlinepubs/007904975/functions/closedir.html, > it should return -1 and set errno to EBADF. That's a "may fail", not a "shall fail". The only way to impose failure for any argument that's not a valid DIR* is to keep an index of all open DIR*'s and search it. This is wasteful and pointless. We actually do it for dlsym/dlclose and library handles, since POSIX requires it there, but it's awful. > Some of us _do_ check return codes. Getting a segfault doesn't seem > more helpful than a message saying which function failed and why. As You don't get a message unless you actively check the return value and print one yourself. Most callers will not, especially since there is no way closedir can fail except for erroneous usage (except EINTR, but due to historical inconsistency, calling any close function in a context where it could EINTR is a really, really bad idea). As such, most incorrect code will go uncaught if closedir just returns an error; the start of this thread is an example of code that didn't get caught until used with musl. > an amateur, I'm just trying to figure out what to do when I need to > use a function I'm not very familiar with. Reading the manual seems > pointless. Can you suggest a reliable, accessible source of > information? The standard is the best, but your link is an outdated version. Here is the current one: http://pubs.opengroup.org/onlinepubs/9699919799/functions/closedir.html Rich