Whamcloud - gitweb
LU-4044 build: fix 'resource leak' errors 94/12394/3
authorSebastien Buisson <sebastien.buisson@bull.net>
Wed, 2 Oct 2013 06:43:08 +0000 (08:43 +0200)
committerOleg Drokin <oleg.drokin@intel.com>
Mon, 1 Dec 2014 04:18:14 +0000 (04:18 +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>
Reviewed-on: http://review.whamcloud.com/12394

lustre/mdt/mdt_coordinator.c
lustre/mdt/mdt_handler.c
lustre/mgs/mgs_llog.c
lustre/utils/lhsmtool_posix.c

index 5ae0b92..02cef6a 100644 (file)
@@ -1863,7 +1863,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;
@@ -1888,7 +1889,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 7a77d22..f69cfc8 100644 (file)
@@ -4553,7 +4553,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);
@@ -4562,7 +4562,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);
@@ -4601,7 +4601,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);
@@ -4615,7 +4615,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 0c49f67..04bb3d5 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 544ef38..a86952c 100644 (file)
@@ -532,7 +532,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);
@@ -540,10 +540,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);
@@ -570,11 +566,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;
        }
 
        /* Don't read beyond a given extent */
@@ -593,6 +589,12 @@ static int ct_copy_data(struct hsm_copyaction_private *hcp, const char *src,
 
        errno = 0;
 
+       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) {
@@ -657,14 +659,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;
 }