Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 24 Jan 2014 15:34:04 -0600
From:      Bryan Drewery <bryan@shatow.net>
To:        Bruce Evans <brde@optusnet.com.au>
Cc:        bapt@FreeBSD.org, freebsd-standards@FreeBSD.org, Jilles Tjoelker <jilles@stack.nl>
Subject:   Re: closedir(3) handling NULL
Message-ID:  <20140124213404.GC73838@admin.xzibition.com>
In-Reply-To: <20140125041504.Y986@besplex.bde.org>
References:  <20140124014105.GC37334@admin.xzibition.com> <20140124132435.GA90996@stack.nl> <20140124165509.GA73838@admin.xzibition.com> <20140125041504.Y986@besplex.bde.org>

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

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

On Sat, Jan 25, 2014 at 06:00:08AM +1100, Bruce Evans wrote:
> On Fri, 24 Jan 2014, Bryan Drewery wrote:
>=20
> > 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. PO=
SIX
> >>> [1] specifies that EBADF should be returned if "The dirp argument does
> >>> not refer to an open directory stream"
> >>
> >>> [1] http://pubs.opengroup.org/onlinepubs/009696799/functions/closedir=
=2Ehtml
>=20
> > I do think that improving portability is important. Even against sloppy
> > coding. Applications developed for Linux are fine passing NULL to close=
dir(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 unimproves portability.  FreeBSD intentionally does the opposite for
> fclose(): from fclose(3):

IMHO we should handle NULL gracefully in all places instead of having
hidden surprises.

>=20
> @ NOTES
> @      The fclose() function does not handle NULL arguments; they will re=
sult in
> @      a segmentation violation.  This is intentional - it makes it easie=
r to
> @      make sure programs written under FreeBSD are bug free.  This behav=
iour is
> @      an implementation detail, and programs should not rely upon it.
>=20
> It would be good to do the same thing for garbage stream pointers.
[...]
> % diff --git lib/libc/gen/closedir.c lib/libc/gen/closedir.c
> % index 88ded37..d7a5bdb 100644
> % --- lib/libc/gen/closedir.c
> % +++ lib/libc/gen/closedir.c
> % @@ -53,6 +53,11 @@ fdclosedir(DIR *dirp)
> %  {
> %  	int fd;
> %=20
> % +	if (dirp =3D=3D NULL) {
> % +		errno =3D EBADF;
> % +		return (-1);
> % +	}
> % +
>=20
> Style bug (extra blank line).
>=20
> %  	if (__isthreaded)
> %  		_pthread_mutex_lock(&dirp->dd_lock);
>=20
> Example of normal style (no extra blank line after an if statement).
>=20
> Extra blank lines are especially not needed after return statements since
> return statements obviously don't fall through.
>=20
> %  	fd =3D dirp->dd_fd;
> % @@ -71,6 +76,10 @@ fdclosedir(DIR *dirp)
> %  int
> %  closedir(DIR *dirp)
> %  {
> % +	int fd;
> % +
> % +	if ((fd =3D fdclosedir(dirp)) =3D=3D -1)
> % +		return (-1);
> %
>=20
> Style bug (extra blank line).
>=20
> There is no man page for fdclosedir(3).  It is in directory(3), but someo=
ne
> forgot to add fdclosedir.3 to MLINKS.  fdopendir.3 is unsorted in MLINKS
> instead of missing.

Thanks for the style comments. I am now aware.

I am fixing the MLINKS issues.

>=20
> % -	return (_close(fdclosedir(dirp)));
> % +	return (_close(fd));
> %  }


Bryan Drewery

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

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

iQJ8BAEBCgBmBQJS4txLXxSAAAAAAC4AKGlzc3Vlci1mcHJAbm90YXRpb25zLm9w
ZW5wZ3AuZmlmdGhob3JzZW1hbi5uZXQzNkZFQkU5OTJGNTI4MERGNDgxMTM2MkE2
RTc4MkFDMDNDOUIwQ0Y5AAoJEG54KsA8mwz5bT0P+QEJzLChOFBCg7IYb26thkhM
lUy1L/EKwPiGDNQTrNUY4SiXhW0SuZDpa+8MkoRMT5yYqXbMpAOApJLt+2c5bYq0
88Q4TIyVPLnHC9HNM6wtneu802VNx2I2eN/xR4VU6SMKIiwI7mWglIWbi4IldnvI
8rsXb5l3rELrHduekyuJk2qbvY6NnBaL8JB7MJ4F4LDKrToYWV/ZBAiVSANDEcTA
L9f27lRK8Z3JgPVngkdZMhU8x0Dmzho7AyJByfu+YxgPvhNSi7vlBBPbCasYTsIn
vit/zLXB8zh26T1MPfMpSvK1ktFUt4nZ+aqfb7Pvv413oTRD3VVnaDetXX5gipI7
ZL1+GJuGr87IW0skvaM6MwaFqcASSyry7UNKW8JZoS7RjpW0wIRvvJw9wnMMnCER
rxSKJNgVJe4QvWvha1IQrGQjBUlOMruAOWfRq5RhcSAd9UHMeHgjXJEoTBWVuuEW
4+Q3wkeU6/KGIp5546xglCqd7BXomS2yWZXinZe851latXXZfXlZHtgPJb0Dda4H
bAzfTxMVoDPhQ/yLfPeph69CVSU+NQLWslnrWM3DGd0imD16o+t+gR3yhjA+GtJY
/vWF7ZvYiQIPOzSA7BhLEETYCPwxfgRZeOxAfLjSiZArxhl16tvdPKAgFtrR9xk7
psJzIJ0fwXnVOsli9tUn
=8/QF
-----END PGP SIGNATURE-----

--TiqCXmo5T1hvSQQg--



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