diff --git a/extras/scandir.c b/extras/scandir.c index a4a3427..1485d6e 100644 --- a/extras/scandir.c +++ b/extras/scandir.c @@ -56,18 +56,26 @@ scandir(const char *dir, struct dirent ***namelist, while (NULL != readdir(d)) count++; - + + closedir(d); + names = malloc(sizeof (struct dirent *) * count); + if (!names) + return -1; - closedir(d); d = opendir(dir); - if (NULL == d) + if (NULL == d) { + free(names); return -1; + } while (NULL != (current = readdir(d))) { if (NULL == select || select(current)) { struct dirent *copyentry = malloc(current->d_reclen); - + /* FIXME: OOM, silently skip it?*/ + if (!copyentry) + continue; + memcpy(copyentry, current, current->d_reclen); names[pos] = copyentry; diff --git a/extras/strutil.c b/extras/strutil.c index f0727b5..f318888 100644 --- a/extras/strutil.c +++ b/extras/strutil.c @@ -13,7 +13,7 @@ char *strstr(char *s1, char *s2) if (*s2 == '\0') /* everything matches empty string */ return s1; - for (p = s1; (p = strchr(p, *s2)) != NULL; p = strchr(p + 1, *s2)) { + for (p = s1; (p = strchr(p, *s2)) != NULL; p++) { if (strncmp(p, s2, len) == 0) return (p); } diff --git a/src/index_dir.c b/src/index_dir.c index 99cc306..435b1b4 100644 --- a/src/index_dir.c +++ b/src/index_dir.c @@ -383,6 +383,9 @@ int main(int argc, char *argv[]) timeptr = gmtime(&timep); #endif now = strdup(asctime(timeptr)); + if (!now) { + return -1; + } now[strlen(now) - 1] = '\0'; #ifdef USE_LOCALTIME printf("\n