From a283c7600e79c84404c8a9dcfcdc05a7d6d17fdb Mon Sep 17 00:00:00 2001 From: Arshad Hussain Date: Tue, 5 Sep 2023 16:02:15 +0530 Subject: [PATCH] LU-17000 coverity: Fix Resource Leak(0) 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 Change-Id: I5c0152014f987264df17fac78390a2afc12c9255 Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/52272 Tested-by: jenkins Tested-by: Maloo Reviewed-by: Andreas Dilger Reviewed-by: Timothy Day Reviewed-by: Oleg Drokin --- lustre/utils/lfs.c | 1 + lustre/utils/liblustreapi_layout.c | 4 +++- lustre/utils/liblustreapi_util.c | 15 ++++++++++----- lustre/utils/lsnapshot.c | 20 +++++++++++++------- lustre/utils/obd.c | 37 +++++++++++++++++++++++-------------- 5 files changed, 50 insertions(+), 27 deletions(-) diff --git a/lustre/utils/lfs.c b/lustre/utils/lfs.c index f3f8101..a6d965a 100644 --- a/lustre/utils/lfs.c +++ b/lustre/utils/lfs.c @@ -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; } } diff --git a/lustre/utils/liblustreapi_layout.c b/lustre/utils/liblustreapi_layout.c index 2dab94e..8155578 100644 --- a/lustre/utils/liblustreapi_layout.c +++ b/lustre/utils/liblustreapi_layout.c @@ -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); diff --git a/lustre/utils/liblustreapi_util.c b/lustre/utils/liblustreapi_util.c index 9cb9c45..9309aac 100644 --- a/lustre/utils/liblustreapi_util.c +++ b/lustre/utils/liblustreapi_util.c @@ -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; diff --git a/lustre/utils/lsnapshot.c b/lustre/utils/lsnapshot.c index 67d99c7..00716e9 100644 --- a/lustre/utils/lsnapshot.c +++ b/lustre/utils/lsnapshot.c @@ -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; } diff --git a/lustre/utils/obd.c b/lustre/utils/obd.c index 936d253..044e9b3 100644 --- a/lustre/utils/obd.c +++ b/lustre/utils/obd.c @@ -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; -- 1.8.3.1