Whamcloud - gitweb
LU-4044 build: fix 'resource leak' errors 20/7820/10
authorSebastien Buisson <sebastien.buisson@bull.net>
Wed, 2 Oct 2013 06:43:08 +0000 (08:43 +0200)
committerOleg Drokin <oleg.drokin@intel.com>
Tue, 19 Nov 2013 14:00:20 +0000 (14:00 +0000)
Fix 'resource leak' defects found by Coverity version 6.6.1:
Resource leak (RESOURCE_LEAK)
Variable going out of scope leaks the storage it points to.

Signed-off-by: Sebastien Buisson <sebastien.buisson@bull.net>
Signed-off-by: Dmitry Eremin <dmitry.eremin@intel.com>
Change-Id: I1039fa8bd5e82acab0ffcb082d35fca28dfb694c
Reviewed-on: http://review.whamcloud.com/7820
Reviewed-by: John L. Hammond <john.hammond@intel.com>
Reviewed-by: jacques-Charles Lafoucriere <jacques-charles.lafoucriere@cea.fr>
Tested-by: Jenkins
Tested-by: Maloo <hpdd-maloo@intel.com>
Reviewed-by: Oleg Drokin <oleg.drokin@intel.com>
lustre/mdt/mdt_coordinator.c
lustre/mdt/mdt_handler.c
lustre/mgs/mgs_llog.c
lustre/utils/lhsmtool_posix.c

index 87a5f3e..3915e21 100644 (file)
@@ -1846,7 +1846,8 @@ static int lprocfs_wr_hsm_policy(struct file *file, const char *buffer,
                RETURN(-ENOMEM);
 
        if (copy_from_user(buf, buffer, count))
-               RETURN(-EFAULT);
+               GOTO(out, rc = -EFAULT);
+
        buf[count] = '\0';
 
        start = buf;
@@ -1871,7 +1872,7 @@ static int lprocfs_wr_hsm_policy(struct file *file, const char *buffer,
                        sz = PAGE_SIZE;
                        OBD_ALLOC(msg, sz);
                        if (!msg)
-                               RETURN(-ENOMEM);
+                               GOTO(out, rc = -ENOMEM);
 
                        hsm_policy_bit2str(0, false, msg, sz);
                        CWARN("%s: '%s' is unknown, "
index 971e728..74a0d80 100644 (file)
@@ -3687,7 +3687,7 @@ static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
        strcpy(name, dev);
        p = strstr(name, "-MDT");
        if (p == NULL)
-               GOTO(cleanup_mem, rc = -ENOMEM);
+               GOTO(free_bufs, rc = -ENOMEM);
        p[3] = 'D';
 
        snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
@@ -3696,7 +3696,7 @@ static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
        if (lprof == NULL || lprof->lp_dt == NULL) {
                CERROR("can't find the profile: %s\n",
                       lustre_cfg_string(cfg, 0));
-               GOTO(cleanup_mem, rc = -EINVAL);
+               GOTO(free_bufs, rc = -EINVAL);
        }
 
        lustre_cfg_bufs_reset(bufs, name);
@@ -3735,7 +3735,7 @@ static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
        /* connect to MDD we just setup */
        rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
        if (rc)
-               RETURN(rc);
+               GOTO(class_detach, rc);
 
        site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
        LASSERT(site);
@@ -3749,7 +3749,7 @@ static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
        snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
        rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
        if (rc)
-               RETURN(rc);
+               GOTO(class_detach, rc);
        mdt->mdt_bottom =
                lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
 
index 3bfd6a8..f6b69ba 100644 (file)
@@ -696,11 +696,11 @@ static int mgs_modify(const struct lu_env *env, struct mgs_device *mgs,
        if (strlcpy(mml->mml_marker.cm_comment, comment,
                    sizeof(mml->mml_marker.cm_comment)) >=
            sizeof(mml->mml_marker.cm_comment))
-               GOTO(out_close, rc = -E2BIG);
+               GOTO(out_free, rc = -E2BIG);
        if (strlcpy(mml->mml_marker.cm_tgtname, devname,
                    sizeof(mml->mml_marker.cm_tgtname)) >=
            sizeof(mml->mml_marker.cm_tgtname))
-               GOTO(out_close, rc = -E2BIG);
+               GOTO(out_free, rc = -E2BIG);
         /* Modify mostly means cancel */
         mml->mml_marker.cm_flags = flags;
         mml->mml_marker.cm_canceltime = flags ? cfs_time_current_sec() : 0;
@@ -709,6 +709,8 @@ static int mgs_modify(const struct lu_env *env, struct mgs_device *mgs,
                          NULL);
        if (!rc && !mml->mml_modified)
                rc = 1;
+
+out_free:
         OBD_FREE_PTR(mml);
 
 out_close:
index 0709264..f182dac 100644 (file)
@@ -507,7 +507,7 @@ static int ct_copy_data(struct hsm_copyaction_private *hcp, const char *src,
        __u64                    offset = hai->hai_extent.offset;
        struct stat              src_st;
        struct stat              dst_st;
-       char                    *buf;
+       char                    *buf = NULL;
        __u64                    write_total = 0;
        __u64                    length;
        time_t                   last_print_time = time(NULL);
@@ -515,10 +515,6 @@ static int ct_copy_data(struct hsm_copyaction_private *hcp, const char *src,
 
        CT_TRACE("going to copy data from '%s' to '%s'", src, dst);
 
-       buf = malloc(opt.o_chunk_size);
-       if (buf == NULL)
-               return -ENOMEM;
-
        if (fstat(src_fd, &src_st) < 0) {
                rc = -errno;
                CT_ERROR(rc, "cannot stat '%s'", src);
@@ -545,11 +541,11 @@ static int ct_copy_data(struct hsm_copyaction_private *hcp, const char *src,
 
        rc = lseek(src_fd, hai->hai_extent.offset, SEEK_SET);
        if (rc < 0) {
-               CT_ERROR(errno,
+               rc = -errno;
+               CT_ERROR(rc,
                         "cannot seek for read to "LPU64" (len %jd) in '%s'",
                         hai->hai_extent.offset, (intmax_t)src_st.st_size, src);
-               rc = -errno;
-               goto out;
+               return rc;
        }
 
        he.offset = offset;
@@ -567,6 +563,12 @@ static int ct_copy_data(struct hsm_copyaction_private *hcp, const char *src,
        /* Don't read beyond a given extent */
        length = min(hai->hai_extent.length, src_st.st_size);
 
+       buf = malloc(opt.o_chunk_size);
+       if (buf == NULL) {
+               rc = -ENOMEM;
+               goto out;
+       }
+
        CT_DEBUG("Going to copy "LPU64" bytes %s -> %s\n", length, src, dst);
 
        while (write_total < length) {
@@ -631,14 +633,14 @@ out:
                rc = ftruncate(dst_fd, src_st.st_size);
                if (rc < 0) {
                        rc = -errno;
-                       CT_ERROR(rc,
-                                "cannot truncate '%s' to size %jd",
+                       CT_ERROR(rc, "cannot truncate '%s' to size %jd",
                                 dst, (intmax_t)src_st.st_size);
                        err_major++;
                }
        }
 
-       free(buf);
+       if (buf != NULL)
+               free(buf);
 
        return rc;
 }