Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 24 Jan 2014 20:06:35 +0200
From:      Konstantin Belousov <kostikbel@gmail.com>
To:        Bryan Drewery <bryan@shatow.net>
Cc:        bapt@FreeBSD.org, freebsd-standards@FreeBSD.org, Jilles Tjoelker <jilles@stack.nl>
Subject:   Re: closedir(3) handling NULL
Message-ID:  <20140124180635.GN24664@kib.kiev.ua>
In-Reply-To: <20140124174938.GB73838@admin.xzibition.com>
References:  <20140124014105.GC37334@admin.xzibition.com> <20140124132435.GA90996@stack.nl> <20140124165509.GA73838@admin.xzibition.com> <20140124172123.GK24664@kib.kiev.ua> <20140124174938.GB73838@admin.xzibition.com>

next in thread | previous in thread | raw e-mail | index | archive | help

--YuJye9aIuN0w6xGV
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Fri, Jan 24, 2014 at 11:49:39AM -0600, Bryan Drewery wrote:
> On Fri, Jan 24, 2014 at 07:21:23PM +0200, Konstantin Belousov wrote:
> > On Fri, Jan 24, 2014 at 10:55:09AM -0600, Bryan Drewery wrote:
> > > On Fri, Jan 24, 2014 at 02:24:35PM +0100, Jilles Tjoelker wrote:
> > > > On Thu, Jan 23, 2014 at 07:41:05PM -0600, Bryan Drewery wrote:
> > > > > I found that Linux handles closedir(NULL) fine and returns EINVAL=
=2E POSIX
> > > > > [1] specifies that EBADF should be returned if "The dirp argument=
 does
> > > > > not refer to an open directory stream"
> > > >=20
> > > > > [1] http://pubs.opengroup.org/onlinepubs/009696799/functions/clos=
edir.html
> > > >=20
> > > > > I've updated fdclosedir(3) as well to behave the same.
> > > >=20
> > > > > I'll also update the manpage if there is no objection.
> > > >=20
> > > > If you do this, it is to improve compatibility with poorly written
> > > > software and not for POSIX compliance. POSIX only permits passing n=
ull
> > > > pointers where explicitly specified (e.g. time()); otherwise, passi=
ng a
> > > > null pointer is undefined behaviour like passing any argument outsi=
de
> > > > the required domain.
> > >=20
> > > I do think that improving portability is important. Even against slop=
py
> > > coding. Applications developed for Linux are fine passing NULL to clo=
sedir(3),
> > > which leads to a style of coding that does not reveal itself to be a
> > > problem on FreeBSD until an edge case comes up.
> > >=20
> > > This is the situation to led to me find this. A mountpoint disappeared
> > > and some code written for Linux, that ported to FreeBSD without chang=
es,
> > > segfaulted in closedir(3).
> > This is somewhat strange description of events, it definitely misses
> > some intermediate steps.  The mere fact of unmounting does not change
> > anonymous memory content of some application.  Even if the DIR * was
> > assotiated with the directory descriptor belonging to the disappeared
> > mount point, the DIR * and file descriptors are still valid, although
> > not that functional.
>=20
> The code constructed a path, called opendir(3), received NULL, skipped
> over code that uses the entry since it was NULL, then called
> closedir(3).
>=20
> Also, I don't find this very different than free(3) with a NULL. It's
> considered bad practice to check for NULL before calling it, why is
> closedir(3) any different?

Ok, so it does not have much to do with unmount, it is just an open
of nonexistent path.

I do not have an answer to the question about free(3), except that
free(NULL) behaviour is required by ANSI C, while closedir(NULL) is not,
by posix.

--YuJye9aIuN0w6xGV
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.22 (FreeBSD)

iQIcBAEBAgAGBQJS4quqAAoJEJDCuSvBvK1BOxcP/0HhDJ+9Ao2cRO+c74eDpqKC
ZH2lbY439MnNc321rZL5wgKQoFFWJ6Wsr9iTuSnN5Mz++HHcz1gkIJ4b5EeZ+CXI
JS+VsM2nRSZHEFTrY6Lf6XX8TosBv4eFEyTHQTiIll6oAta4sjA6DieOjwNXmlh/
ggTdmpymZxDofHAMWR+bOYq0yUTJ/kdAVu5OT6iKlgenEwaNY2wweTZdjq/a/cGH
6YS8o48MH14ohvXaw9icdlqaiqvswgvJxT+SrQfdxSCPSbP+cdSR3hGnfoGLICBU
At/B/XlJT9HdLz81q9t/W1ULnof8YPP5NOU9MNBf2tazEwiOrAIIJ54WPQdNJnF4
DxbwHFyXGIV0zP8M7cdA42ZJw0rIIlu7ua53lBwjCPhzIBLYexVWrf5qx/eiJ9cy
4UbKlUFwQB+4wbjizX6lTElUka0APAMaIc5zS1iNUBOsDvsLCf8UubWBjPgHvGBH
bE3zDLX5K4InyEVN64t/oLiyyddyWNdHPPZm5lFAERY7jRFCL5jGTd+Z8jAAVwGh
E+JQ5qFF7i6fYWjalB7qFZt+0ZiBMnxRarqgtC7aMpyBJqHukG3asZs7OeypeBcE
ZIs30y6O0ps3xdXRDY4SribKNX05HklzOpGHx1P4qr1xuJpiDDd8/S8Wd9hXgMRW
FCjLdw5oFm17AbcqI6d+
=ZTYs
-----END PGP SIGNATURE-----

--YuJye9aIuN0w6xGV--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20140124180635.GN24664>