Hello tech@,

I was stealing^W borrowing some code from scandir.c when I noticed some
warnings when compiling with -Wall -Wextra.

It's probably nitpicking, but wouldn't be better declare arraysz as
size_t instead of long?  Or there is something that I'm missing and it
actually makes sense to keep it signed?

Here's more context on scandir.c:

     60 int
     61 scandir(const char *dirname, struct dirent ***namelist,
     62     int (*select)(const struct dirent *),
     63     int (*dcomp)(const struct dirent **, const struct dirent **))
     64 {
     65         struct dirent *d, *p, **names = NULL;
     66         size_t nitems = 0;
     67         struct stat stb;
     68         long arraysz;
     69         DIR *dirp;
    ...
     76         /*
     77          * estimate the array size by taking the size of the directory 
file
     78          * and dividing it by a multiple of the minimum size entry.
     79          */
     80         arraysz = MAXIMUM(stb.st_size / 24, 16);
     81         if (arraysz > SIZE_MAX / sizeof(struct dirent *)) {
     82                 errno = ENOMEM;
     83                 goto fail;
     84         }
     85         names = calloc(arraysz, sizeof(struct dirent *));
    ...
     89         while ((d = readdir(dirp)) != NULL) {
    ...
     93                 /*
     94                  * Check to make sure the array has space left and
     95                  * realloc the maximum size.
     96                  */
     97                 if (nitems >= arraysz) {
    ...
    103                         arraysz *= 2;
    104                         if (SIZE_MAX / sizeof(struct dirent *) < 
arraysz)
    105                                 goto fail;
    106                         nnames = reallocarray(names,
    107                             arraysz, sizeof(struct dirent *));
    ...


Thanks,

Omar Polo


Index: scandir.c
===================================================================
RCS file: /home/cvs/src/lib/libc/gen/scandir.c,v
retrieving revision 1.21
diff -u -p -r1.21 scandir.c
--- scandir.c   28 Jun 2019 13:32:41 -0000      1.21
+++ scandir.c   25 Apr 2021 10:39:15 -0000
@@ -63,9 +63,8 @@ scandir(const char *dirname, struct dire
     int (*dcomp)(const struct dirent **, const struct dirent **))
 {
        struct dirent *d, *p, **names = NULL;
-       size_t nitems = 0;
+       size_t arraysz, nitems = 0;
        struct stat stb;
-       long arraysz;
        DIR *dirp;
 
        if ((dirp = opendir(dirname)) == NULL)

Reply via email to