git: 8ebc0768663b - main - scandir: Code cleanup.

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Thu, 26 Jun 2025 07:38:04 UTC
The branch main has been updated by des:

URL: https://cgit.FreeBSD.org/src/commit/?id=8ebc0768663b0ea9f78a0b8cffb93ee1684a9b08

commit 8ebc0768663b0ea9f78a0b8cffb93ee1684a9b08
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2025-06-26 07:37:06 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2025-06-26 07:37:06 +0000

    scandir: Code cleanup.
    
    Sponsored by:   Klara, Inc.
    Reviewed by:    markj
    Differential Revision:  https://reviews.freebsd.org/D51050
---
 lib/libc/gen/scandir.c | 24 +++++++++++++-----------
 1 file changed, 13 insertions(+), 11 deletions(-)

diff --git a/lib/libc/gen/scandir.c b/lib/libc/gen/scandir.c
index 56d77c29bd07..8e62fe980868 100644
--- a/lib/libc/gen/scandir.c
+++ b/lib/libc/gen/scandir.c
@@ -69,8 +69,8 @@ scandir_dirp_b(DIR *dirp, struct dirent ***namelist, select_block select,
     dcomp_block dcomp)
 #else
 scandir_dirp(DIR *dirp, struct dirent ***namelist,
-    int (*select)(const struct dirent *), int (*dcomp)(const struct dirent **,
-    const struct dirent **))
+    int (*select)(const struct dirent *),
+    int (*dcomp)(const struct dirent **, const struct dirent **))
 #endif
 {
 	struct dirent *d, *p = NULL, **names = NULL, **names2;
@@ -100,7 +100,7 @@ scandir_dirp(DIR *dirp, struct dirent ***namelist,
 		 * realloc the maximum size.
 		 */
 		if (numitems >= arraysz) {
-			arraysz = arraysz ? arraysz * 2 : 32;
+			arraysz = arraysz * 2;
 			names2 = reallocarray(names, arraysz, sizeof(*names));
 			if (names2 == NULL)
 				goto fail;
@@ -115,13 +115,15 @@ scandir_dirp(DIR *dirp, struct dirent ***namelist,
 	 */
 	if (errno != 0)
 		goto fail;
-	if (numitems && dcomp != NULL)
+	if (numitems > 0 && dcomp != NULL) {
 #ifdef I_AM_SCANDIR_B
-		qsort_b(names, numitems, sizeof(struct dirent *), (void*)dcomp);
+		qsort_b(names, numitems, sizeof(struct dirent *),
+		    (void *)dcomp);
 #else
 		qsort_r(names, numitems, sizeof(struct dirent *),
 		    scandir_thunk_cmp, &dcomp);
 #endif
+	}
 	*namelist = names;
 	return (numitems);
 
@@ -142,8 +144,8 @@ scandir_b(const char *dirname, struct dirent ***namelist, select_block select,
     dcomp_block dcomp)
 #else
 scandir(const char *dirname, struct dirent ***namelist,
-    int (*select)(const struct dirent *), int (*dcomp)(const struct dirent **,
-    const struct dirent **))
+    int (*select)(const struct dirent *),
+    int (*dcomp)(const struct dirent **, const struct dirent **))
 #endif
 {
 	DIR *dirp;
@@ -171,8 +173,8 @@ fdscandir_b(int dirfd, struct dirent ***namelist, select_block select,
     dcomp_block dcomp)
 #else
 fdscandir(int dirfd, struct dirent ***namelist,
-    int (*select)(const struct dirent *), int (*dcomp)(const struct dirent **,
-    const struct dirent **))
+    int (*select)(const struct dirent *),
+    int (*dcomp)(const struct dirent **, const struct dirent **))
 #endif
 {
 	DIR *dirp;
@@ -200,8 +202,8 @@ scandirat_b(int dirfd, const char *dirname, struct dirent ***namelist,
     select_block select, dcomp_block dcomp)
 #else
 scandirat(int dirfd, const char *dirname, struct dirent ***namelist,
-    int (*select)(const struct dirent *), int (*dcomp)(const struct dirent **,
-    const struct dirent **))
+    int (*select)(const struct dirent *),
+    int (*dcomp)(const struct dirent **, const struct dirent **))
 #endif
 {
 	int fd, ret, serrno;