diff options
author | Eric Andersen <andersen@codepoet.org> | 2002-01-08 09:00:17 +0000 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 2002-01-08 09:00:17 +0000 |
commit | 351727917b49ddd9e5b7924e8cb7d95c126e4200 (patch) | |
tree | a11e4b70c0cb9d5d7d711d78654d71ec85449859 /libc/misc/dirent/scandir64.c | |
parent | b3f5e468f2c257dac3121984c096ade30e3adf30 (diff) |
Scrub malloc handling
Diffstat (limited to 'libc/misc/dirent/scandir64.c')
-rw-r--r-- | libc/misc/dirent/scandir64.c | 21 |
1 files changed, 13 insertions, 8 deletions
diff --git a/libc/misc/dirent/scandir64.c b/libc/misc/dirent/scandir64.c index 6c6697b42..12752db72 100644 --- a/libc/misc/dirent/scandir64.c +++ b/libc/misc/dirent/scandir64.c @@ -36,11 +36,6 @@ #include "dirstream.h" -/* - * FIXME: This is a simple hack version which doesn't sort the data, and - * just passes all unsorted. - */ - int scandir64(const char *dir, struct dirent64 ***namelist, int (*selector) (const struct dirent64 *), int (*compar) (const __ptr_t, const __ptr_t)) @@ -58,7 +53,10 @@ int scandir64(const char *dir, struct dirent64 ***namelist, while (NULL != readdir64(d)) count++; - names = malloc(sizeof (struct dirent64 *) * count); + if (!(names = malloc(sizeof (struct dirent64 *) * count))) { + closedir(d); + return -1; + } rewinddir(d); @@ -74,8 +72,15 @@ int scandir64(const char *dir, struct dirent64 ***namelist, } result = closedir(d); - if (pos != count) - names = realloc(names, sizeof (struct dirent64 *) * pos); + if (pos != count) { + struct dirent64 **tmp; + if (!(tmp = realloc(names, sizeof (struct dirent64 *) * pos))) { + free(names); + return -1; + } + names = tmp; + } + if (compar != NULL) { qsort(names, pos, sizeof (struct dirent64 *), compar); |