diff --git a/lib/libc/gen/scandir.3 b/lib/libc/gen/scandir.3 --- a/lib/libc/gen/scandir.3 +++ b/lib/libc/gen/scandir.3 @@ -25,7 +25,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd June 19, 2025 +.Dd June 20, 2025 .Dt SCANDIR 3 .Os .Sh NAME @@ -102,7 +102,8 @@ It returns the number of entries in the array. A pointer to the array of directory entries is stored in the location referenced by -.Fa namelist . +.Fa namelist +(even if no entries were selected). .Pp The .Fa select diff --git a/lib/libc/gen/scandir.c b/lib/libc/gen/scandir.c --- a/lib/libc/gen/scandir.c +++ b/lib/libc/gen/scandir.c @@ -74,9 +74,13 @@ #endif { struct dirent *d, *p = NULL, **names = NULL, **names2; - size_t arraysz = 0, numitems = 0; + size_t arraysz = 32, numitems = 0; int serrno; + names = malloc(arraysz * sizeof(*names)); + if (names == NULL) + return (-1); + while ((d = readdir(dirp)) != NULL) { if (select != NULL && !SELECT(d)) continue; /* just selected names */ diff --git a/lib/libc/tests/gen/scandir_test.c b/lib/libc/tests/gen/scandir_test.c --- a/lib/libc/tests/gen/scandir_test.c +++ b/lib/libc/tests/gen/scandir_test.c @@ -103,10 +103,32 @@ ATF_REQUIRE_EQ(0, close(fd)); } +static int +scandir_none(const struct dirent *ent __unused) +{ + return (0); +} + +ATF_TC(scandir_none); +ATF_TC_HEAD(scandir_none, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test scandir() when no entries are selected"); +} +ATF_TC_BODY(scandir_none, tc) +{ + struct dirent **namelist = NULL; + + ATF_REQUIRE_EQ(0, scandir(".", &namelist, scandir_none, alphasort)); + ATF_REQUIRE(namelist); + free(namelist); +} + ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, scandir_test); ATF_TP_ADD_TC(tp, fscandir_test); ATF_TP_ADD_TC(tp, scandirat_test); + ATF_TP_ADD_TC(tp, scandir_none); return (atf_no_error()); }