diff options
author | Guillem Jover <guillem@debian.org> | 2009-11-09 21:16:07 +0100 |
---|---|---|
committer | Guillem Jover <guillem@debian.org> | 2009-11-09 21:22:22 +0100 |
commit | 5ad592f7eff684e44149aff8efa4190d2e81f6f8 (patch) | |
tree | 9944770df9cfd4d2b60672f93729a70d786a4039 /lib/compat | |
parent | 365611d9f86e1da98007e5b3b3a14dfea2d5acda (diff) | |
download | dpkg-5ad592f7eff684e44149aff8efa4190d2e81f6f8.tar.gz |
libcompat: Do not preallocate list before the loop in scandir
Let the realloc in the loop take care of it once it's needed, this way
we get rid of an additional point of failure.
Diffstat (limited to 'lib/compat')
-rw-r--r-- | lib/compat/scandir.c | 13 |
1 files changed, 6 insertions, 7 deletions
diff --git a/lib/compat/scandir.c b/lib/compat/scandir.c index 2fc8d017a..ea7c484e8 100644 --- a/lib/compat/scandir.c +++ b/lib/compat/scandir.c @@ -57,12 +57,8 @@ scandir(const char *dir, struct dirent ***namelist, if (!d) return -1; - used = 0; - avail = 20; - - list = malloc(avail * sizeof(struct dirent *)); - if (!list) - return cleanup(d, list, used); + list = NULL; + used = avail = 0; while ((e = readdir(d)) != NULL) { if (filter != NULL && !filter(e)) @@ -71,7 +67,10 @@ scandir(const char *dir, struct dirent ***namelist, if (used >= avail - 1) { struct dirent **newlist; - avail += avail; + if (avail) + avail *= 2; + else + avail = 20; newlist = realloc(list, avail * sizeof(struct dirent *)); if (!newlist) return cleanup(d, list, used); |