[comp.bugs.sys5] Bug in closedir

bill@twwells.com (T. William Wells) (09/16/89)

Closedir, on my Microport SysV/386 3.0e, frees the DIR structure
and then closes the file using the first field of that structure.
If you are using -lmalloc, which trashes the freed data, you are
going to be unpleasantly surprised.

No doubt this bogosity is present throughout the library.

---
Bill                    { uunet | novavax | ankh | sunvice } !twwells!bill
bill@twwells.com

gwyn@smoke.BRL.MIL (Doug Gwyn) (09/17/89)

In article <1989Sep16.025206.5915@twwells.com> bill@twwells.com (T. William Wells) writes:
>Closedir, on my Microport SysV/386 3.0e, frees the DIR structure
>and then closes the file using the first field of that structure.
>... No doubt this bogosity is present throughout the library.

I don't know about the last statement.  I suspect Microport is using AT&T's
closedir() implementation, the SVR3.0 version of which was derived from one
of my earlier public-domain implementations.  R. Salz pointed out the bug
to me and I fixed it long ago; here is my current version:

/*
	closedir -- close a directory stream

	last edit:	11-Nov-1988	D A Gwyn
*/

#include	<sys/errno.h>
#include	<sys/types.h>
#include	<dirent.h>

typedef char	*pointer;		/* (void *) if you have it */

extern void	free();
extern int	close();

extern int	errno;

#ifndef NULL
#define	NULL	0
#endif

int
closedir( dirp )
	register DIR	*dirp;		/* stream from opendir() */
	{
	register int	fd;

	if ( dirp == NULL || dirp->dd_buf == NULL )
		{
		errno = EFAULT;
		return -1;		/* invalid pointer */
		}

	fd = dirp->dd_fd;		/* bug fix thanks to R. Salz */
	free( (pointer)dirp->dd_buf );
	free( (pointer)dirp );
	return close( fd );
	}

bill@twwells.com (T. William Wells) (09/17/89)

In article <11081@smoke.BRL.MIL> gwyn@brl.arpa (Doug Gwyn) writes:
: In article <1989Sep16.025206.5915@twwells.com> bill@twwells.com (T. William Wells) writes:
: >Closedir, on my Microport SysV/386 3.0e, frees the DIR structure
: >and then closes the file using the first field of that structure.
: >... No doubt this bogosity is present throughout the library.
:
: I don't know about the last statement.  I suspect Microport is using AT&T's
: closedir() implementation, the SVR3.0 version of which was derived from one
: of my earlier public-domain implementations.  R. Salz pointed out the bug
: to me and I fixed it long ago; here is my current version:

The closedir on my system consists of just the three calls: two frees
and a close. There is no error checking of any kind.

---
Bill                    { uunet | novavax | ankh | sunvice } !twwells!bill
bill@twwells.com

guy@auspex.auspex.com (Guy Harris) (09/19/89)

>: I don't know about the last statement.  I suspect Microport is using AT&T's
>: closedir() implementation, the SVR3.0 version of which was derived from one
>: of my earlier public-domain implementations.  R. Salz pointed out the bug
>: to me and I fixed it long ago; here is my current version:
>
>The closedir on my system consists of just the three calls: two frees
>and a close. There is no error checking of any kind.

The "closedir" in AT&T's S5R3.[01] implementation consists of just the
three calls: two frees and a close.  There is no error checking of any
kind, unless you count the fact that the return value of the "close" is
the return value of "closedir".... 

In other words, your observation about the contents of your system's
"closedir" is not at all inconsistent with Doug's hypothesis that
Microport is using AT&T's standard "closedir".