From: Sebastien Buisson Date: Tue, 11 Sep 2012 07:43:22 +0000 (+0200) Subject: LU-1884 build: fix 'resource leak' errors X-Git-Tag: 2.3.51~87 X-Git-Url: https://git.whamcloud.com/?p=fs%2Flustre-release.git;a=commitdiff_plain;h=1ba7eb520d6f50908985b45cb1aecad83bdf9c63 LU-1884 build: fix 'resource leak' errors Fix 'resource leak' defects found by Coverity version 6.0.3: Resource leak (RESOURCE_LEAK) Variable going out of scope leaks the storage it points to. Signed-off-by: Sebastien Buisson Change-Id: I6fe04d30c209d164ac8c741497c675a33ffcca6d Reviewed-on: http://review.whamcloud.com/3934 Tested-by: Hudson Reviewed-by: Bob Glossman Reviewed-by: Keith Mannthey Tested-by: Maloo Reviewed-by: Oleg Drokin --- diff --git a/libcfs/libcfs/linux/linux-proc.c b/libcfs/libcfs/linux/linux-proc.c index 5294e08..b093d12 100644 --- a/libcfs/libcfs/linux/linux-proc.c +++ b/libcfs/libcfs/linux/linux-proc.c @@ -176,8 +176,10 @@ static int __proc_dobitmasks(void *data, int write, } } else { rc = cfs_trace_copyin_string(tmpstr, tmpstrlen, buffer, nob); - if (rc < 0) - return rc; + if (rc < 0) { + cfs_trace_free_string_buffer(tmpstr, tmpstrlen); + return rc; + } rc = libcfs_debug_str2mask(mask, tmpstr, is_subsys); /* Always print LBUG/LASSERT to console, so keep this mask */ diff --git a/lustre/llite/llite_lib.c b/lustre/llite/llite_lib.c index 9980c2c..e05431e 100644 --- a/lustre/llite/llite_lib.c +++ b/lustre/llite/llite_lib.c @@ -2146,9 +2146,12 @@ int ll_prep_inode(struct inode **inode, ibits = MDS_INODELOCK_LAYOUT; if (S_ISREG(md.body->mode) && sbi->ll_flags & LL_SBI_LAYOUT_LOCK && md.lsm != NULL && !ll_have_md_lock(*inode, &ibits, LCK_MINMODE)) { + char *fsname = ll_get_fsname(*inode); CERROR("%s: inode "DFID" (%p) layout lock not granted.\n", - ll_get_fsname(*inode), PFID(ll_inode2fid(*inode)), + fsname, PFID(ll_inode2fid(*inode)), *inode); + if (fsname) + OBD_FREE(fsname, MGS_PARAM_MAXLEN); } out: diff --git a/lustre/lmv/lmv_intent.c b/lustre/lmv/lmv_intent.c index 6bb2fb8..d3caecb 100644 --- a/lustre/lmv/lmv_intent.c +++ b/lustre/lmv/lmv_intent.c @@ -577,8 +577,10 @@ int lmv_revalidate_slaves(struct obd_export *exp, struct ptlrpc_request **reqp, * cached all the time. */ obj = lmv_object_find_lock(obd, mid); - if (obj == NULL) - RETURN(-EALREADY); + if (obj == NULL) { + OBD_FREE_PTR(op_data); + RETURN(-EALREADY); + } for (i = 0; i < obj->lo_objcount; i++) { fid = obj->lo_stripes[i].ls_fid; diff --git a/lustre/obdfilter/filter_log.c b/lustre/obdfilter/filter_log.c index d997ae8..626ab8f 100644 --- a/lustre/obdfilter/filter_log.c +++ b/lustre/obdfilter/filter_log.c @@ -177,6 +177,7 @@ static int filter_recov_log_unlink_cb(struct llog_ctxt *ctxt, if (oa->o_seq > FID_SEQ_OST_MAX) { CERROR("%s: invalid group number "LPU64" > MAX_CMD_GROUP %u\n", exp->exp_obd->obd_name, oa->o_seq, FID_SEQ_OST_MAX); + OBDO_FREE(oa); RETURN(-EINVAL); } diff --git a/lustre/ptlrpc/service.c b/lustre/ptlrpc/service.c index 672704d..c614eaa 100644 --- a/lustre/ptlrpc/service.c +++ b/lustre/ptlrpc/service.c @@ -729,6 +729,8 @@ ptlrpc_register_service(struct ptlrpc_service_conf *conf, if (rc <= 0) { CERROR("%s: failed to parse CPT array %s: %d\n", conf->psc_name, cconf->cc_pattern, rc); + if (cpts != NULL) + OBD_FREE(cpts, sizeof(*cpts) * ncpts); RETURN(ERR_PTR(rc < 0 ? rc : -EINVAL)); } ncpts = rc; diff --git a/lustre/tests/flocks_test.c b/lustre/tests/flocks_test.c index 9450958..d738f1f 100644 --- a/lustre/tests/flocks_test.c +++ b/lustre/tests/flocks_test.c @@ -132,6 +132,7 @@ int t1(int argc, char *argv[]) int fd; int mount_with_flock = 0; int error = 0; + int rc = 0; if (argc != 5) { t1_usage(); @@ -167,13 +168,19 @@ int t1(int argc, char *argv[]) error = flock(fd, LOCK_EX); } else { t1_usage(); - return EXIT_FAILURE; + rc = EXIT_FAILURE; + goto out; } if (mount_with_flock) - return((error == 0) ? EXIT_SUCCESS : EXIT_FAILURE); + rc = ((error == 0) ? EXIT_SUCCESS : EXIT_FAILURE); else - return((error == 0) ? EXIT_FAILURE : EXIT_SUCCESS); + rc = ((error == 0) ? EXIT_FAILURE : EXIT_SUCCESS); + +out: + if (fd >= 0) + close(fd); + return rc; } /** =============================================================== @@ -244,7 +251,8 @@ int t2(int argc, char* argv[]) rc = t_fcntl(fd, F_GETFL); if ((rc & O_APPEND) == 0) { fprintf(stderr, "error get flag: ret %x\n", rc); - return EXIT_FAILURE; + rc = EXIT_FAILURE; + goto out; } ta.lock = &lock; diff --git a/lustre/utils/lfs.c b/lustre/utils/lfs.c index 436a341..851fb6f 100644 --- a/lustre/utils/lfs.c +++ b/lustre/utils/lfs.c @@ -2404,7 +2404,7 @@ static int flushctx_ioctl(char *mp) static int lfs_flushctx(int argc, char **argv) { int kdestroy = 0, c; - FILE *proc; + FILE *proc = NULL; char procline[PATH_MAX], *line; int rc = 0; @@ -2447,7 +2447,8 @@ static int lfs_flushctx(int argc, char **argv) fprintf(stderr, "%s: unexpected format in " "/proc/mounts\n", argv[0]); - return -1; + rc = -1; + goto out; } if (strcmp(fs, "lustre") != 0) @@ -2469,6 +2470,9 @@ static int lfs_flushctx(int argc, char **argv) } } +out: + if (proc != NULL) + fclose(proc); return rc; } @@ -2753,14 +2757,12 @@ static int lfs_data_version(int argc, char **argv) if (rc) { fprintf(stderr, "can't get version for %s: %s\n", path, strerror(errno = -rc)); - return rc; - } - - printf(LPU64 "\n", data_version); + } else + printf(LPU64 "\n", data_version); close(fd); - return 0; + return rc; } int main(int argc, char **argv) diff --git a/lustre/utils/liblustreapi.c b/lustre/utils/liblustreapi.c index aea7389..cd75334 100644 --- a/lustre/utils/liblustreapi.c +++ b/lustre/utils/liblustreapi.c @@ -410,6 +410,8 @@ static int get_param_obdvar(const char *fsname, const char *file_path, llapi_error(LLAPI_MSG_ERROR, rc, "'%s' is not on a Lustre filesystem", file_path); + if (fp != NULL) + fclose(fp); return rc; } } else if (fsname) { @@ -1016,7 +1018,7 @@ int llapi_get_poollist(const char *name, char **poollist, int list_size, rc = -errno; llapi_error(LLAPI_MSG_ERROR, rc, "Error reading pool list for '%s'", name); - return rc; + goto out; } else if ((rc == 0) && (cookie == NULL)) { /* end of directory */ break; @@ -1027,13 +1029,17 @@ int llapi_get_poollist(const char *name, char **poollist, int list_size, continue; /* check output bounds */ - if (nb_entries >= list_size) - return -EOVERFLOW; + if (nb_entries >= list_size) { + rc = -EOVERFLOW; + goto out; + } /* +2 for '.' and final '\0' */ - if (used + strlen(pool.d_name) + strlen(fsname) + 2 - > buffer_size) - return -EOVERFLOW; + if (used + strlen(pool.d_name) + strlen(fsname) + 2 + > buffer_size) { + rc = -EOVERFLOW; + goto out; + } sprintf(buffer + used, "%s.%s", fsname, pool.d_name); poollist[nb_entries] = buffer + used; @@ -1041,8 +1047,9 @@ int llapi_get_poollist(const char *name, char **poollist, int list_size, nb_entries++; } +out: closedir(dir); - return nb_entries; + return ((rc != 0) ? rc : nb_entries); } /* wrapper for lfs.c and obd.c */ @@ -1606,8 +1613,10 @@ retry_get_uuids: if (ret == -EOVERFLOW) { uuids_temp = realloc(uuids, obdcount * sizeof(struct obd_uuid)); - if (uuids_temp != NULL) + if (uuids_temp != NULL) { + uuids = uuids_temp; goto retry_get_uuids; + } else ret = -ENOMEM; } @@ -3068,7 +3077,7 @@ static int rmtacl_notify(int ops) { FILE *fp; struct mntent *mnt; - int found = 0, fd, rc; + int found = 0, fd = 0, rc = 0; fp = setmntent(MOUNTED, "r"); if (fp == NULL) { @@ -3083,7 +3092,7 @@ static int rmtacl_notify(int ops) if (!mnt) break; - if (!llapi_is_lustre_mnt(mnt)) + if (!llapi_is_lustre_mnt(mnt)) continue; fd = open(mnt->mnt_dir, O_RDONLY | O_DIRECTORY); @@ -3091,20 +3100,24 @@ static int rmtacl_notify(int ops) rc = -errno; llapi_error(LLAPI_MSG_ERROR, rc, "Can't open '%s'\n", mnt->mnt_dir); - return rc; + goto out; } rc = ioctl(fd, LL_IOC_RMTACL, ops); if (rc < 0) { rc = -errno; llapi_error(LLAPI_MSG_ERROR, rc, "ioctl %d\n", fd); - return rc; + goto out; } found++; } + +out: endmntent(fp); - return found; + if (fd >= 0) + close(fd); + return ((rc != 0) ? rc : found); } static char *next_token(char *p, int div) diff --git a/lustre/utils/llverfs.c b/lustre/utils/llverfs.c index 953f342..8200826 100644 --- a/lustre/utils/llverfs.c +++ b/lustre/utils/llverfs.c @@ -527,6 +527,7 @@ static int dir_write(char *chunk_buf, size_t chunksize, struct timeval start_time; unsigned long long total_bytes; unsigned long long curr_bytes = 0; + int rc = 0; #ifdef HAVE_EXT2FS_EXT2FS_H if (!full && fsetflags(testdir, EXT2_TOPDIR_FL)) @@ -545,7 +546,8 @@ static int dir_write(char *chunk_buf, size_t chunksize, fflush(countfile) != 0) { fprintf(stderr, "\n%s: writing %s failed :%s\n", progname, filecount, strerror(errno)); - return 6; + rc = 6; + goto out; } /* calculate total bytes that need to be written */ @@ -553,7 +555,8 @@ static int dir_write(char *chunk_buf, size_t chunksize, if (total_bytes <= 0) { fprintf(stderr, "\n%s: unable to calculate total bytes\n", progname); - return 7; + rc = 7; + goto out; } if (!full && (dir_num != 0)) @@ -575,7 +578,8 @@ static int dir_write(char *chunk_buf, size_t chunksize, fprintf(stderr, "\n%s: mkdir %s : %s\n", progname, tempdir, strerror(errno)); - return 1; + rc = 1; + goto out; } } dir_num++; @@ -600,8 +604,10 @@ static int dir_write(char *chunk_buf, size_t chunksize, time_st, inode_st, tempfile); close(fd); if (ret < 0) { - if (ret != -ENOSPC) - return 1; + if (ret != -ENOSPC) { + rc = 1; + goto out; + } curr_bytes = total_bytes; break; } @@ -618,7 +624,6 @@ static int dir_write(char *chunk_buf, size_t chunksize, progname, filecount, strerror(errno)); } } - fclose(countfile); if (verbose) { verbose++; @@ -628,7 +633,10 @@ static int dir_write(char *chunk_buf, size_t chunksize, verbose--; } - return 0; +out: + fclose(countfile); + + return rc; } /* diff --git a/lustre/utils/lustre_rsync.c b/lustre/utils/lustre_rsync.c index 43fa7b4..85c8a1f 100644 --- a/lustre/utils/lustre_rsync.c +++ b/lustre/utils/lustre_rsync.c @@ -1386,7 +1386,7 @@ int lr_replicate() { void *changelog_priv; struct lr_info *info; - struct lr_info *ext; + struct lr_info *ext = NULL; time_t start; int xattr_not_supp; int i; @@ -1402,15 +1402,17 @@ int lr_replicate() if (rc) { fprintf(stderr, "Source path is not a valid Lustre client " "mountpoint.\n"); - return rc; + goto out; } if (status->ls_mdt_device[0] == '\0') snprintf(status->ls_mdt_device, LR_NAME_MAXLEN, "%s%s", status->ls_source_fs, DEFAULT_MDT); ext = calloc(1, sizeof(struct lr_info)); - if (ext == NULL) - return -ENOMEM; + if (ext == NULL) { + rc = -ENOMEM; + goto out; + } for (i = 0, xattr_not_supp = 0; i < status->ls_num_targets; i++) { snprintf(info->dest, PATH_MAX, "%s/%s", status->ls_targets[i], @@ -1419,7 +1421,8 @@ int lr_replicate() if (rc == -1 && errno != EEXIST) { fprintf(stderr, "Error writing to target path %s.\n", status->ls_targets[i]); - return -errno; + rc = -errno; + goto out; } rc = llistxattr(info->src, info->xlist, info->xsize); if (rc == -1 && errno == ENOTSUP) { @@ -1440,7 +1443,7 @@ int lr_replicate() if (rc < 0) { fprintf(stderr, "Error opening changelog file for fs %s.\n", status->ls_source_fs); - return rc; + goto out; } while (!quit && lr_parse_line(changelog_priv, info) == 0) { @@ -1523,7 +1526,15 @@ int lr_replicate() printf("Changelog records consumed: %lld\n", rec_count); } - return 0; + rc = 0; + +out: + if (info != NULL) + free(info); + if (ext != NULL) + free(ext); + + return rc; } void diff --git a/lustre/utils/mount_utils_ldiskfs.c b/lustre/utils/mount_utils_ldiskfs.c index 5554e84..c71c6a7 100644 --- a/lustre/utils/mount_utils_ldiskfs.c +++ b/lustre/utils/mount_utils_ldiskfs.c @@ -150,6 +150,7 @@ int ldiskfs_write_ldd(struct mkfs_opts *mop) if (num < 1 && ferror(filep)) { fprintf(stderr, "%s: Unable to write to file (%s): %s\n", progname, filepnm, strerror(errno)); + fclose(filep); goto out_umnt; } fclose(filep); @@ -1023,8 +1024,10 @@ static char *absolute_path(char *devname) return NULL; if (devname[0] != '/') { - if (getcwd(buf, sizeof(buf) - 1) == NULL) + if (getcwd(buf, sizeof(buf) - 1) == NULL) { + free(path); return NULL; + } strcat(buf, "/"); strcat(buf, devname); } else {