Whamcloud - gitweb
LU-17000 coverity: Fix Resource Leak(0) 72/52272/4
authorArshad Hussain <arshad.hussain@aeoncomputing.com>
Tue, 5 Sep 2023 10:32:15 +0000 (16:02 +0530)
committerOleg Drokin <green@whamcloud.com>
Thu, 28 Sep 2023 08:00:45 +0000 (08:00 +0000)
This patch fixes Resource leak error reported
by coverity run.

CoverityID: 339696 ("Resource Leak"): liblustreapi_layout.c
CoverityID: 397918 ("Resource Leak"): lsnapshot.c
CoverityID: 397894 ("Resource Leak"): obd.c
CoverityID: 397851 ("Resource Leak"): lfs.c
CoverityID: 397832 ("Resource Leak"): liblusteapi.c
CoverityID: 397772 ("Resource Leak"): liblusteapi_utils.c
CoverityID: 397721 ("Resource Leak"): obd.c

Test-Parameters: trivial fstype=zfs testlist=sanity,conf-sanity,sanity-lsnapshot
Signed-off-by: Arshad Hussain <arshad.hussain@aeoncomputing.com>
Change-Id: I5c0152014f987264df17fac78390a2afc12c9255
Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/52272
Tested-by: jenkins <devops@whamcloud.com>
Tested-by: Maloo <maloo@whamcloud.com>
Reviewed-by: Andreas Dilger <adilger@whamcloud.com>
Reviewed-by: Timothy Day <timday@amazon.com>
Reviewed-by: Oleg Drokin <green@whamcloud.com>
lustre/utils/lfs.c
lustre/utils/liblustreapi_layout.c
lustre/utils/liblustreapi_util.c
lustre/utils/lsnapshot.c
lustre/utils/obd.c

index f3f8101..a6d965a 100644 (file)
@@ -861,6 +861,7 @@ static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int),
                rc = ftruncate(fd_dst, pos);
                if (rc < 0) {
                        rc = -errno;
+                       free(buf);
                        return rc;
                }
        }
index 2dab94e..8155578 100644 (file)
@@ -3031,8 +3031,10 @@ int llapi_mirror_resync_many_params(int fd, struct llapi_layout *layout,
                        if (pos >= mirror_end || !src) {
                                rc = llapi_mirror_find(layout, pos, end,
                                                        &mirror_end);
-                               if (rc < 0)
+                               if (rc < 0) {
+                                       free(buf);
                                        return rc;
+                               }
                                src = rc;
                                /* restrict mirror end by resync end */
                                mirror_end = MIN(end, mirror_end);
index 9cb9c45..9309aac 100644 (file)
@@ -362,10 +362,15 @@ int llapi_direntry_remove(char *dname)
        int rc = 0;
 
        dirpath = strdup(dname);
-       namepath = strdup(dname);
-       if (!dirpath || !namepath)
+       if (!dirpath)
                return -ENOMEM;
 
+       namepath = strdup(dname);
+       if (!namepath) {
+               rc = -ENOMEM;
+               goto out_dirpath;
+       }
+
        filename = basename(namepath);
 
        dir = dirname(dirpath);
@@ -383,10 +388,10 @@ int llapi_direntry_remove(char *dname)
                            "error on ioctl %#lx for '%s' (%d)",
                            (long)LL_IOC_LMV_SETSTRIPE, filename, fd);
 out:
-       free(dirpath);
+       close(fd);
        free(namepath);
-       if (fd != -1)
-               close(fd);
+out_dirpath:
+       free(dirpath);
        return rc;
 #else
        return -EOPNOTSUPP;
index 67d99c7..00716e9 100644 (file)
@@ -559,7 +559,7 @@ static int snapshot_load_conf(struct snapshot_instance *si, int lock_mode)
                        "Can't fdopen the snapshot config file %s: %s\n",
                        conf_name, strerror(errno));
                rc = -1;
-               goto out;
+               goto out_fd;
        }
 
        while (snapshot_fgets(fp, buf, MAX_BUF_SIZE) != NULL) {
@@ -608,14 +608,18 @@ static int snapshot_load_conf(struct snapshot_instance *si, int lock_mode)
 
 out:
        if (fd >= 0) {
-               if (rc < 0) {
+               if (rc < 0)
                        flock(fd, LOCK_UN);
-                       close(fd);
-               } else {
+               else
                        si->si_conf_fd = fd;
-               }
        }
-
+       /* fclose() closes any associated underlying file descriptor.
+        * explicit close(fd) not required
+        */
+       fclose(fp);
+       return rc;
+out_fd:
+       close(fd);
        return rc;
 }
 
@@ -1242,8 +1246,10 @@ again:
 
                rc = scnprintf(cmd + len, size - len - 1,
                               "-o %s=\"%s\" ", buf, ptr);
-               if (rc <= 0)
+               if (rc <= 0) {
+                       pclose(fp);
                        return -EOVERFLOW;
+               }
 
                len += rc;
        }
index 936d253..044e9b3 100644 (file)
@@ -3293,30 +3293,37 @@ static int llog_search_ost_cb(const char *record, void *data)
        char *ostname = data;
        char ost_filter[MAX_STRING_SIZE] = {'\0'};
        char *add_osc, *del_osc, *setup, *cleanup;
+       int rc = 0;
 
        add_osc = get_event_filter(LCFG_LOV_ADD_OBD);
        del_osc = get_event_filter(LCFG_LOV_DEL_OBD);
        setup = get_event_filter(LCFG_SETUP);
        cleanup = get_event_filter(LCFG_CLEANUP);
-       if (!add_osc || !del_osc || !setup || !cleanup)
-               return -ENOMEM;
+       if (!add_osc || !del_osc || !setup || !cleanup) {
+               rc = -ENOMEM;
+               goto out;
+       }
 
        if (ostname && ostname[0])
                snprintf(ost_filter, sizeof(ost_filter), " %s,", ostname);
 
        if (strstr(record, ost_filter)) {
-               if (strstr(record, add_osc) || strstr(record, setup))
-                       return 1;
-               if (strstr(record, del_osc) || strstr(record, cleanup))
-                       return -ENOENT;
+               if (strstr(record, add_osc) || strstr(record, setup)) {
+                       rc = 1;
+                       goto out_setup;
+               }
+               if (strstr(record, del_osc) || strstr(record, cleanup)) {
+                       rc = -ENOENT;
+                       goto out_setup;
+               }
        }
-
-       free(add_osc);
-       free(del_osc);
-       free(setup);
+out_setup:
        free(cleanup);
-
-       return 0;
+       free(setup);
+       free(del_osc);
+       free(add_osc);
+out:
+       return rc;
 }
 
 /**
@@ -5034,8 +5041,10 @@ static int llog_poollist_cb(const char *record, void *data)
                        name = strstr(record, type);
                        /* 2 bytes for " }" */
                        name_len = strlen(name) - type_len - 2;
-                       if (name_len <= 0 || name_len > sizeof(tmp->lpn_name))
-                               return -EINVAL;
+                       if (name_len <= 0 || name_len > sizeof(tmp->lpn_name)) {
+                               rc = -EINVAL;
+                               goto out;
+                       }
                        tmp = malloc(sizeof(struct llog_pool_name));
                        if (!tmp) {
                                rc = -ENOMEM;