Whamcloud - gitweb
LU-17000 utils: Fix negative argument passed 96/53796/10
authorArshad Hussain <arshad.hussain@aeoncomputing.com>
Mon, 22 Jan 2024 16:09:19 +0000 (21:39 +0530)
committerOleg Drokin <green@whamcloud.com>
Tue, 25 Jun 2024 03:25:42 +0000 (03:25 +0000)
This patch fixes bunch of "Argument cannot be negative"
reported by Coverity.

CoverityID: 397713 ("Argument cannot be negative")
CoverityID: 397899 ("Argument cannot be negative")
Pass -rc to strerror() since rc is guaranteed to be negative.

CoverityID: 397769 ("Argument cannot be negative")
read() could return -1 on error. Check before using the value

CoverityID: 397780 ("Argument cannot be negative")
Use static allocation removing the need to call sysconf()

CoverityID: 403104 ("Argument cannot be negative")
On failure to open do not try to call close()

Fixes: 58d744e3 (LU-10092 pcc: Non-blocking PCC caching)
Fixes: f172b116 (LU-10092 llite: Add persistent cache on client)
Fixes: aed82919 (LU-16029 utils: add options to lr_reader to parse raw files)
Fixes: 86ba46c2 (LU-9680 obdclass: user netlink to collect devices information)
Signed-off-by: Arshad Hussain <arshad.hussain@aeoncomputing.com>
Change-Id: Id51c6c184d30dce596e7ab948a6fd0768eed1503
Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/53796
Tested-by: jenkins <devops@whamcloud.com>
Tested-by: Maloo <maloo@whamcloud.com>
Reviewed-by: James Simmons <jsimmons@infradead.org>
Reviewed-by: Oleg Drokin <green@whamcloud.com>
Reviewed-by: Andreas Dilger <adilger@whamcloud.com>
Reviewed-by: Timothy Day <timday@amazon.com>
lustre/utils/lfs.c
lustre/utils/liblustreapi_pcc.c
lustre/utils/liblustreapi_util.c
lustre/utils/lr_reader.c
lustre/utils/lustre_cfg.c

index 5eb9628..c83c6fc 100755 (executable)
@@ -13751,7 +13751,7 @@ static int lfs_pcc_attach_fid(int argc, char **argv)
                        fprintf(stderr,
                                "%s: cannot attach '%s' on '%s' to PCC with attach ID '%u': %s\n",
                                argv[0], fidstr, mntpath, attach_id,
-                               strerror(rc2));
+                               strerror(-rc2));
                }
                if (rc == 0 && rc2 < 0)
                        rc = rc2;
index c80e7d1..cb1e312 100644 (file)
@@ -463,10 +463,9 @@ out:
  */
 int llapi_pccdev_get(const char *mntpath)
 {
-       long page_size = sysconf(_SC_PAGESIZE);
        char pathbuf[sizeof(struct obd_uuid)];
+       char buf[65536]; /* large engough to hold PPC dev list */
        glob_t path;
-       char *buf;
        int fd;
        int rc;
 
@@ -491,17 +490,8 @@ int llapi_pccdev_get(const char *mntpath)
                goto out_free_param;
        }
 
-       buf = calloc(1, page_size);
-       if (buf == NULL) {
-               rc = -ENOMEM;
-               llapi_error(LLAPI_MSG_ERROR, rc,
-                           "error: pccdev_get: allocating '%s' buffer",
-                           path.gl_pathv[0]);
-               goto out_close;
-       }
-
        while (1) {
-               ssize_t count = read(fd, buf, page_size);
+               ssize_t count = read(fd, buf, sizeof(buf));
 
                if (count == 0)
                        break;
@@ -521,9 +511,10 @@ int llapi_pccdev_get(const char *mntpath)
                        break;
                }
        }
-out_close:
+
        close(fd);
-       free(buf);
+       cfs_free_param_data(&path);
+       return rc;
 out_free_param:
        cfs_free_param_data(&path);
        return rc;
index ceab7a4..817664f 100644 (file)
@@ -388,8 +388,8 @@ int llapi_direntry_remove(char *dname)
                llapi_error(LLAPI_MSG_ERROR, errno,
                            "error on ioctl %#lx for '%s' (%d)",
                            (long)LL_IOC_LMV_SETSTRIPE, filename, fd);
-out:
        close(fd);
+out:
        free(namepath);
 out_dirpath:
        free(dirpath);
index 3c07de4..b4824ce 100644 (file)
@@ -79,6 +79,12 @@ static void dump_log(int fd)
 
        do {
                n = read(fd, buf, sizeof(buf));
+               if (n < 0) {
+                       fprintf(stderr,
+                               "%s: dump_log() Failed to read: %s\n",
+                               progname, strerror(errno));
+                       exit(errno);
+               }
                n = write(2, buf, n);
        } while (n == sizeof(buf));
 
index a0f4e79..cd5dce8 100644 (file)
@@ -1640,10 +1640,9 @@ static int do_name2dev(char *func, char *name, int dev_id)
 int parse_devname(char *func, char *name, int dev_id)
 {
        int rc = 0;
-       int ret = -1;
 
        if (!name)
-               return ret;
+               return -EINVAL;
 
        /* Test if its a pure number string */
        if (strspn(name, "0123456789") != strlen(name)) {
@@ -1651,19 +1650,17 @@ int parse_devname(char *func, char *name, int dev_id)
                        name++;
 
                rc = do_name2dev(func, name, dev_id);
-               if (rc >= 0)
-                       ret = rc;
        } else {
                errno = 0;
-               ret = strtoul(name, NULL, 10);
+               rc = strtoul(name, NULL, 10);
                if (errno)
-                       rc = errno;
+                       rc = -errno;
        }
 
        if (rc < 0)
                fprintf(stderr, "No device found for name %s: %s\n",
-                       name, strerror(rc));
-       return ret;
+                       name, strerror(-rc));
+       return rc;
 }
 
 #ifdef HAVE_SERVER_SUPPORT