Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Thu, 31 Jan 2019 21:30:07 -0500
From: Rich Felker <>
Subject: Re: closedir()

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:
> > >
> >
> > 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
> 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:


Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.