zos: return on realloc failure in scandir()

Fixes: https://github.com/libuv/libuv/issues/2692
PR-URL: https://github.com/libuv/libuv/pull/2693
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
This commit is contained in:
Milad Farazmand 2020-03-10 11:29:44 +01:00 committed by Ben Noordhuis
parent af7143b6f2
commit b5155dd2ac

View File

@ -62,19 +62,15 @@ int scandir(const char* maindir, struct dirent*** namelist,
if (!filter || filter(dirent)) { if (!filter || filter(dirent)) {
struct dirent* copy; struct dirent* copy;
copy = uv__malloc(sizeof(*copy)); copy = uv__malloc(sizeof(*copy));
if (!copy) { if (!copy)
while (count) { goto error;
dirent = nl[--count];
uv__free(dirent);
}
uv__free(nl);
closedir(mdir);
errno = ENOMEM;
return -1;
}
memcpy(copy, dirent, sizeof(*copy)); memcpy(copy, dirent, sizeof(*copy));
nl = uv__realloc(nl, sizeof(*copy) * (count + 1)); nl = uv__realloc(nl, sizeof(*copy) * (count + 1));
if (nl == NULL) {
uv__free(copy);
goto error;
}
nl[count++] = copy; nl[count++] = copy;
} }
} }
@ -86,6 +82,16 @@ int scandir(const char* maindir, struct dirent*** namelist,
*namelist = nl; *namelist = nl;
return count; return count;
error:
while (count > 0) {
dirent = nl[--count];
uv__free(dirent);
}
uv__free(nl);
closedir(mdir);
errno = ENOMEM;
return -1;
} }