summaryrefslogtreecommitdiffstats
path: root/cpukit/libcsupport/src
diff options
context:
space:
mode:
authorJoel Sherrill <joel.sherrill@OARcorp.com>2001-11-30 12:03:15 +0000
committerJoel Sherrill <joel.sherrill@OARcorp.com>2001-11-30 12:03:15 +0000
commitc43d7bd117323e82cdd96f7f34c9cd1f34ce065d (patch)
tree5b49978e0efcbf313080e78e5759c48dcfa00c9a /cpukit/libcsupport/src
parent2001-11-28 Ralf Corsepius <corsepiu@faw.uni-ulm.de> (diff)
downloadrtems-c43d7bd117323e82cdd96f7f34c9cd1f34ce065d.tar.bz2
2001-11-30 Jennifer Averett <jennifer@OARcorp.com>
This was tracked as PR88. * libc/scandir.c: Fixed to perform cleanup on error conditions.
Diffstat (limited to 'cpukit/libcsupport/src')
-rw-r--r--cpukit/libcsupport/src/scandir.c30
1 files changed, 23 insertions, 7 deletions
diff --git a/cpukit/libcsupport/src/scandir.c b/cpukit/libcsupport/src/scandir.c
index 01e17c197f..ff527b13b4 100644
--- a/cpukit/libcsupport/src/scandir.c
+++ b/cpukit/libcsupport/src/scandir.c
@@ -83,16 +83,19 @@ scandir(dirname, namelist, select, dcomp)
int (*select) __P((struct dirent *));
int (*dcomp) __P((const void *, const void *));
{
- register struct dirent *d, *p, **names;
+ register struct dirent *d = NULL;
+ register struct dirent *p = NULL;
+ register struct dirent **names = NULL;
register size_t nitems;
struct stat stb;
long arraysz;
- DIR *dirp;
+ DIR *dirp = NULL;
+ int i;
if ((dirp = opendir(dirname)) == NULL)
return(-1);
if (fstat(dirp->dd_fd, &stb) < 0)
- return(-1);
+ goto cleanup_and_bail;
/*
* estimate the array size by taking the size of the directory file
@@ -101,7 +104,7 @@ scandir(dirname, namelist, select, dcomp)
arraysz = (stb.st_size / 24);
names = (struct dirent **)malloc(arraysz * sizeof(struct dirent *));
if (names == NULL)
- return(-1);
+ goto cleanup_and_bail;
nitems = 0;
while ((d = readdir(dirp)) != NULL) {
@@ -112,7 +115,7 @@ scandir(dirname, namelist, select, dcomp)
*/
p = (struct dirent *)malloc(DIRSIZ(d));
if (p == NULL)
- return(-1);
+ goto cleanup_and_bail;
p->d_ino = d->d_ino;
p->d_reclen = d->d_reclen;
p->d_namlen = d->d_namlen;
@@ -123,12 +126,12 @@ scandir(dirname, namelist, select, dcomp)
*/
if (++nitems >= arraysz) {
if (fstat(dirp->dd_fd, &stb) < 0)
- return(-1); /* just might have grown */
+ goto cleanup_and_bail; /* just might have grown */
arraysz = stb.st_size / 12;
names = (struct dirent **)realloc((char *)names,
arraysz * sizeof(struct dirent *));
if (names == NULL)
- return(-1);
+ goto cleanup_and_bail;
}
names[nitems-1] = p;
}
@@ -138,6 +141,19 @@ scandir(dirname, namelist, select, dcomp)
}
*namelist = names;
return(nitems);
+
+cleanup_and_bail:
+
+ if ( dirp )
+ closedir( dirp );
+
+ if ( names ) {
+ for (i=0; i < nitems; i++ )
+ free( names[i] );
+ free( names );
+ }
+
+ return(-1);
}
/*