diff options
author | Joel Sherrill <joel.sherrill@OARcorp.com> | 2001-11-30 12:03:15 +0000 |
---|---|---|
committer | Joel Sherrill <joel.sherrill@OARcorp.com> | 2001-11-30 12:03:15 +0000 |
commit | c43d7bd117323e82cdd96f7f34c9cd1f34ce065d (patch) | |
tree | 5b49978e0efcbf313080e78e5759c48dcfa00c9a /c | |
parent | 2001-11-28 Ralf Corsepius <corsepiu@faw.uni-ulm.de> (diff) | |
download | rtems-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 '')
-rw-r--r-- | c/src/exec/libcsupport/src/scandir.c | 30 | ||||
-rw-r--r-- | c/src/lib/ChangeLog | 5 | ||||
-rw-r--r-- | c/src/lib/libc/scandir.c | 30 | ||||
-rw-r--r-- | cpukit/libcsupport/src/scandir.c | 30 |
4 files changed, 74 insertions, 21 deletions
diff --git a/c/src/exec/libcsupport/src/scandir.c b/c/src/exec/libcsupport/src/scandir.c index 01e17c197f..ff527b13b4 100644 --- a/c/src/exec/libcsupport/src/scandir.c +++ b/c/src/exec/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); } /* diff --git a/c/src/lib/ChangeLog b/c/src/lib/ChangeLog index 5d1f476c1f..40bea96de9 100644 --- a/c/src/lib/ChangeLog +++ b/c/src/lib/ChangeLog @@ -1,3 +1,8 @@ +2001-11-30 Jennifer Averett <jennifer@OARcorp.com> + + This was tracked as PR88. + * libc/scandir.c: Fixed to perform cleanup on error conditions. + 2001-11-07 Jennifer Averett <jennifer@OARcorp.com> Reported by Ibragimov Ilya <ibr@oktet.ru> and tracked as PR63. diff --git a/c/src/lib/libc/scandir.c b/c/src/lib/libc/scandir.c index 01e17c197f..ff527b13b4 100644 --- a/c/src/lib/libc/scandir.c +++ b/c/src/lib/libc/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); } /* 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); } /* |